JavaScript Array copyWithin()

Examples

Copy to index 2, all elements from index 0:

const fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.copyWithin(2, 0);
Try it Yourself »

Copy to index 2, the elements from index 0 to 2:

const fruits = ["Banana", "Orange", "Apple", "Mango", "Kiwi"];
fruits.copyWithin(2, 0, 2);
Try it Yourself »

Description

The copyWithin() method copies array elements to another position in an array.

The copyWithin() method overwrites the existing values.

The copyWithin() method does not add items to the array.


Syntax

array.copyWithin(target, start, end)

Parameters

Parameter Description
target Required.
The index (position) to copy the elements to.
start Optional.
The start index (position). Default is 0.
end Optional.
The end index (position). Default is the array length.

Return Value

Type Description
ArrayThe changed array.


Browser Support

copyWithin() is an ECMAScript6 (ES6) feature.

ES6 (JavaScript 2015) is supported in all modern browsers since June 2017:

Chrome 51 Edge 15 Firefox 54 Safari 10 Opera 38
May 2016 Apr 2017 Jun 2017 Sep 2016 Jun 2016

copyWithin() is not supported in Internet Explorer.


Copyright 1999-2023 by Refsnes Data. All Rights Reserved.