JavaScript Array copyWithin() method

The JavaScript Array copyWithin() method shallow copies array elements to another position in the array, overwriting the existing values.

The arr.copyWithin() method copies part of an array to the same array and returns it, without modifying its size i.e, copies array element of an array within the same array.

The copyWithin() method copies array elements to another position in the 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: This method accepts three parameters as mentioned above and described below:  

target – The position where the copied element takes place. If negative, target will be counted from the end.

start – It is optional. It represents the index from where the method starts copying elements. By default, it is 0.

If start is omitted, copyWithin will copy from index 0. If negative, start will be counted from the end.

end – It is optional. It represents the index at which elements stops copying. By default, it is array.length-1.

If end is omitted, copyWithin will copy until the last index (default to arr.length).

If any of the arguments are negative, index will be counted from backwards. For example, -1 represents the last element and so on.

If target value is after start, the copied sequence is trimmed to fit arr.length.

Return Value

It returns the modified array.

Below are the example of the Array copyWithin() method. 

const array1 = ['a', 'b', 'c', 'd', 'e'];

// copy to index 0 the element at index 3
console.log(array1.copyWithin(0, 3, 4));
// expected output: Array ["d", "b", "c", "d", "e"]

// copy to index 1 all elements from index 3 to the end
console.log(array1.copyWithin(1, 3));
// expected output: Array ["d", "d", "e", "d", "e"]
[1, 2, 3, 4, 5].copyWithin(-2)
// [1, 2, 3, 1, 2]

[1, 2, 3, 4, 5].copyWithin(0, 3)
// [4, 5, 3, 4, 5]

[1, 2, 3, 4, 5].copyWithin(0, 3, 4)
// [4, 2, 3, 4, 5]

[1, 2, 3, 4, 5].copyWithin(-2, -3, -1)
// [1, 2, 3, 3, 4]

[].copyWithin.call({length: 5, 3: 1}, 0, 3)
// {0: 1, 3: 1, length: 5}

// ES2015 Typed Arrays are subclasses of Array
var i32a = new Int32Array([1, 2, 3, 4, 5])

i32a.copyWithin(0, 2)
// Int32Array [3, 4, 5, 4, 5]

// On platforms that are not yet ES2015 compliant:
[].copyWithin.call(new Int32Array([1, 2, 3, 4, 5]), 0, 3, 4);
// Int32Array [4, 2, 3, 4, 5]
let array = [1, 2, 3, 4, 5, 6];

// target: from second-to-last element, start: 0, end: array.length
let returned_arr = array.copyWithin(-2);
console.log(returned_arr); // [ 1, 2, 3, 4, 1, 2 ]
// modifies the original array
console.log(array); // [ 1, 2, 3, 4, 1, 2 ]

array = [1, 2, 3, 4, 5, 6];
// target: 0, start copying from 5th element
array.copyWithin(0, 4);
console.log(array); // [ 5, 6, 3, 4, 5, 6 ]

array = [1, 2, 3, 4, 5, 6];
// target: 1, start copying from 3rd element to second-to-last element
array.copyWithin(1, 2, -1); // -1 = last element (exclusive)
console.log(array); // [ 1, 3, 4, 5, 5, 6 ]