JavaScript Array copyWithin()
Example
Copy the first two array elements to the last two array elements:
const fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.copyWithin(2, 0);
Try it Yourself »
More "Try it Yourself" examples below.
Definition and Usage
The copyWithin() method copies array elements
to another position in an array, overwriting the existing values.
The copyWithin() does not add items to the array.
copyWithin() overwrites the original array.
Browser Support
The numbers in the table specify the first browser version that fully supports the method.
| Chrome 45 | Edge 12 | Firefox 32 | Safari 9 | Opera 32 |
| Sep 2015 | Jul 2015 | Sep 2014 | Sep 2015 | Sep 2015 |
The copyWithin() method is not supported in Internet Explorer.
Syntax
array.copyWithin(target, start, end)
Parameter Values
| Parameter | Description |
|---|---|
| target | Required. The index position to copy the 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: | An Array, the changed array |
|---|---|
| JavaScript Version: | ECMAScript 6 |
More Examples
Example
Copy the first two array elements to the third and fourth position:
const fruits = ["Banana", "Orange", "Apple", "Mango", "Kiwi"];
fruits.copyWithin(2, 0, 2);
Try it Yourself »

