Array.prototype.copyWithin()

Copies part of an array to another location in the same array, in place.

Since ES2015 Spec ↗

Syntax

arr.copyWithin(target, start, end)

Parameters

NameTypeRequiredDescription
target number Yes Index at which to paste the copied sequence. Negative counts from the end.
start number No Index to start copying from. Defaults to 0. Negative counts from the end.
end number No Index before which to stop copying (exclusive). Defaults to array length.

Returns

Array — The modified array (same reference).

Examples

console.log([1, 2, 3, 4, 5].copyWithin(0, 3));
Output
[ 4, 5, 3, 4, 5 ]
console.log([1, 2, 3, 4, 5].copyWithin(1, 3, 4));
Output
[ 1, 4, 3, 4, 5 ]

Notes

Mutates the array in place and never changes its length. Rarely used outside of performance-sensitive typed-array code.

Browser & runtime support

EnvironmentSince version
chrome 45
firefox 32
safari 9
edge 12
node 4.0

See also