JavaScript Array copyWithin()
Examples
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 »
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 »
Definition and Usage
The copyWithin()
method copies array elements to another position in the array.
The copyWithin()
method overwrites the existing values.
The copyWithin()
method does not add items to the array.
Syntax
array.copyWithin(target, start, end)
Parameters
Parameter | Description |
target | Required. The index (position) to copy the elements to. |
start | Optional. The start index (position). Default is 0. |
end | Optional. The end index (position). Default is the array length. |
Return Value
Type | Description |
An array | The changed array. |
Browser Support
copyWithin()
is an ECMAScript6 (ES6) feature.
ES6 (JavaScript 2015) is supported in all modern browsers:
Chrome | Edge | Firefox | Safari | Opera |
Yes | Yes | Yes | Yes | Yes |
copyWithin()
is not supported in Internet Explorer 11 (or earlier).