JavaScript (ES2026) reference · Array Methods
JavaScript Array copyWithin() Method
Copy a slice of the array to another position in the same array, overwriting what's there. Changes the array in place.
JavaScript (ES2026) Since ES2015
Syntax
array.copyWithin(target, start, end)
Parameters
| Parameter | Description |
|---|---|
target |
Index to copy to. |
start optional |
Index to start copying from. Default: 0 |
end optional |
Index to stop copying before. Default: array.length |
Return value
The same, modified array.
Examples
const nums = [1, 2, 3, 4, 5];
nums.copyWithin(0, 3);
console.log(nums);
Output
[ 4, 5, 3, 4, 5 ]
console.log([1, 2, 3, 4, 5].copyWithin(1, 0, 2));
Output
[ 1, 1, 2, 4, 5 ]
Version history
| Version | Change |
|---|---|
| JavaScript ES2015 | Added |
Related
array.fill()· Fill all or part of an array with a static value, changing it in place. Often used to create an array of defaults.array.splice()· Remove, replace or insert elements at a position, changing the array in place. Returns the removed elements.- JavaScript Arrays · tutorial
- JavaScript Array Methods · tutorial