JavaScript Array copyWithin() Method


The JavaScript array copyWithin() method copies the part of the given array with its own elements and returns the modified array. This method doesn't change the length of the modified array.

The copyWithin() method copies array elements to another position in the array, overwriting the existing values.

This method will never add more items to the array.

Syntax

array.copyWithin(target, start, end)

Parameter

target - The position where the copied element takes place.

start - It is optional. It represents the index from where the method starts copying elements. By default, it is 0.

end - It is optional. It represents the index at which elements stops copying. By default, it is array.length-1.

Example -