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);
The result of fruits will be:
Banana,Orange,Banana,Orange
Try it Yourself »
More "Try it Yourself" examples below.
Definition and Usage
The copyWithin() method copies array elements within the array, to and from specified positions.
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 | Required. The index position to start copying elements from |
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);
The output of the code above will be:
Banana,Orange,Banana,Orange,Kiwi,Papaya
Try it Yourself »
JavaScript Array Reference