Array.prototype.copyWithin

Copies a section of the array from the start index to the end index into the target index, overwriting the existing array elements starting at the target index. It modifies the original array without resizing it, and returns it.

Syntax

copyWithin(target, start?, end?)

Parameters

Return value

The modified original array.

Examples

   ["A", "B", "C", "D", "E"].copyWithin(0, 2, 4)
//   0    1   [2    3    4)
//   ↑        \______/
//   target    ↑         ↑
//             start     end
[ "C", "D", "C", "D", "E" ]
[0, 1, 2, 3, 4].copyWithin(0, 2, 4)
[ 2, 3, 2, 3, 4 ]
[0, 1, "A", "B", 4].copyWithin(0, 2, 4)
[ "A", "B", "A", "B", 4 ]
[0, 1, 2, 3, 4, 5, "A", "B"].copyWithin(0, -2)
[ "A", "B", 2, 3, 4, 5, "A", "B" ]
[0, 1, 2, 3, 4, 5, "A", "B"].copyWithin(1, -2)
[ 0, "A", "B", 3, 4, 5, "A", "B" ]
["never", "gonna", "give", "you", "up"].copyWithin(3)
[ "never", "gonna", "give", "never", "gonna" ]