Example 1 — Copy a Slice to the Front
Copy indexes 3–5 and paste them starting at index 0.
const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9];
numbers.copyWithin(0, 3, 6);
console.log(numbers);
// [4, 5, 6, 4, 5, 6, 7, 8, 9] How It Works
- Pick the source. Indexes 3, 4, 5 hold
4, 5, 6(endis exclusive). - Pick the target. Writing starts at index
0. - Overwrite three slots. Positions 0–2 become
4, 5, 6. - Length stays 9. The rest of the array is left as-is after the paste window.

