Javascript Array copyWithin()

The copyWithin() method copies array elements from one position to another in the given array.

Example

let words = ["apple", "ball", "cat", "dog"];

// copies element from index 0 to index 3 words.copyWithin(3, 0);
// modifies the original array console.log(words); // Output: // [ ''apple'', ''ball'', ''cat'', ''apple'' ]

copyWithin() Syntax

The syntax of the copyWithin() method is:

arr.copyWithin(target, start, end)

Here, arr is an array.


copyWithin() Parameters

The copyWithin() method can take three parameters:

  • target - The index position to copy the elements to.
  • start (optional) - The index position to start copying elements from. If omitted, it will copy from index 0.
  • end (optional) - The index position to stop copying elements from (end element not included). If omitted, it will copy until the last index.

Notes:

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

copyWithin() Return Value

  • Returns the modified array after copying the elements.

Notes: The copyWithin() method:

  • overwrites the original array.
  • does not change the length of the original array.

Example 1: Using copyWithin() Method

let numbers = [1, 2, 3, 4, 5];

// copying element located at 4th index to 0th index numbers.copyWithin(0, 4);
// modifies the original array console.log(numbers); // [ 5, 6, 3, 4, 5 ] let letters = ["a", "b", "c", "d"];
// copying element located at 1st index to 2nd index letters.copyWithin(2, 1);
// modifies the original array console.log(letters); // [ 'a', 'b', 'b', 'c' ]

Output

[ 5, 2, 3, 4, 5 ]
[ 'a', 'b', 'b', 'c' ]

In the above example, we have used the copyWithin() method to copy the element from one index to another in the arrays- numbers and letters.

numbers.copyWithin(0, 4) copies element located at start i.e. index 4 to target i.e. index 0 and letters.copyWithin(2, 1) copies element from index 1 to index 2.


Example 2: copyWithin() with Three Parameters

let laptops = ["dell", "hp", "acer", "asus"];

// copying elements from index 2 to 4(excluding 4) to index 0 laptops.copyWithin(0, 2, 4);
// modifies the original array console.log(laptops); // [ 'acer', 'asus', 'acer', 'asus' ]

Output

[ 'acer', 'asus', 'acer', 'asus' ]

In the above example, we have passed three parameters- target, start, and end in the copyWithin method.

laptops.copyWithin(0, 2, 4) copies elements from index 2 to 4 (not including index 4) to index 0 and modifies the laptops array to [ 'acer', 'asus', 'acer', 'asus' ].


Example 3: copyWithin() with Negative Parameter

let evenNumbers= [2,4,6,8];

// passing negative index value -1 as target index evenNumbers.copyWithin(-1);
console.log(evenNumbers);

Output

[ 2, 4, 6, 2 ]

Here we have passed a negative index value -1 as the value for target in the copyWithin() method.

Since the passed argument is negative, the method counts the index backward so the target index is the last index.

evenNumbers.copyWithin(-1) copies the element at index 0(default start value) to last index.


Did you find this article helpful?