JavaScript Array copyWithin() Method

Definition and Usage

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

copyWithin() The method never adds more items to the array.

Tip:copyWithin() The method will overwrite the original array.

Example

Example 1

Copy the first two array elements to the last two array elements:

var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.copyWithin(2, 0);

Try It Yourself

Example 2

Copy the first two array elements to the third and fourth positions:

var fruits = ["Banana", "Orange", "Apple", "Mango", "Kiwi", "Papaya"];
fruits.copyWithin(2, 0, 2);

Try It Yourself

Syntax

array.copyWithin(target, start, end)

Parameter Value

Parameter Description
target Required. The index position to copy elements to.
start Optional. The index position to start copying elements from (default is 0).
end Optional. The index position to stop copying elements from (default is array.length).

Technical Details

Return Value: Array, the array being changed.
JavaScript Version: ECMAScript 6

Browser Support

The numbers in the table indicate the first browser version that fully supports this method.

Chrome Edge Firefox Safari Opera
Chrome 45 Edge Firefox 32 Safari 9 Opera 32
September 2015 July 2015 September 2014 September 2015 September 2015

Note:Internet Explorer does not support the copyWithin() method.

Related Pages

Tutorial:JavaScript Array

Tutorial:JavaScript Array Const

Tutorial:JavaScript Array Methods

Tutorial:JavaScript Sort Array

Tutorial:JavaScript Array Iteration