Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

Array.prototype.copyWithin: copying a range inside one array

copyWithin copies elements within the same receiver, preserves its length, and handles overlapping ranges safely.

Version: ECMAScript 2026 draft; example target: Node.js 24.22.2

Syntax

array.copyWithin(target, start[, end])

array.copyWithin(target, start[, end]) mutates the receiver and returns that exact object. It copies [start, end) to target; negative indexes count from the end and fractions are truncated. Length never changes.

The algorithm chooses a copy direction like memmove, so overlapping ranges do not destroy values that are still needed. A source hole deletes the corresponding target property. The method is generic for writable array-like objects; a read-only property or proxy failure can throw TypeError. The example copies the tail into the middle.

Examples

const values = [1, 2, 3, 4, 5];
const returned = values.copyWithin(1, 3);
console.log(values.join(","));
console.log(returned === values, values.length);

Result

1,4,5,4,5
true 5

Sources