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

ParameterDescription
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

Copy the last two to the frontTry it yourself
const nums = [1, 2, 3, 4, 5];
nums.copyWithin(0, 3);
console.log(nums);

Output

[ 4, 5, 3, 4, 5 ]
Copy a rangeTry it yourself
console.log([1, 2, 3, 4, 5].copyWithin(1, 0, 2));

Output

[ 1, 1, 2, 4, 5 ]

Version history

VersionChange
JavaScript ES2015Added