JavaScript Array copyWithin() Method
Example
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 »
More "Try it Yourself" examples below.
Definition and Usage
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.
Note: this method overwrites the original array.
Browser Support
The numbers in the table specify the first browser version that fully supports the method.
Method | |||||
---|---|---|---|---|---|
copyWithin() | 45.0 | 12.0 | 32.0 | 9 | 32.0 |
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:
var fruits = ["Banana", "Orange", "Apple", "Mango", "Kiwi", "Papaya"];
fruits.copyWithin(2, 0, 2);
Try it Yourself »
Related Pages
JavaScript Tutorial: JavaScript Arrays