JavaScript Array copyWithin()
Examples
Copy to index 2, all elements from index 0:
const fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.copyWithin(2, 0);
Try it Yourself »
fruits.copyWithin(2, 0);
Copy to index 2, the elements from index 0 to 2:
const fruits = ["Banana", "Orange", "Apple", "Mango", "Kiwi"];
fruits.copyWithin(2, 0, 2);
Try it Yourself »
fruits.copyWithin(2, 0, 2);
Description
The copyWithin()
method copies array elements to another position in an 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.
The index (position) to copy the elements to.
start
Optional.
The start index (position). Default is 0.
The start index (position). Default is 0.
end
Optional.
The end index (position). Default is the array length.
The end index (position). Default is the array length.
Return Value
Type
Description
Array The changed array.
Array Tutorials:
Browser Support
copyWithin()
is an ECMAScript6 (ES6 2015) feature.
JavaScript 2015 is supported in all browsers since June 2017:
Chrome 51 |
Edge 15 |
Firefox 54 |
Safari 10 |
Opera 38 |
May 2016 | Apr 2017 | Jun 2017 | Sep 2016 | Jun 2016 |