JavaScript Tutorials

JavaScript Array copyWithin()
Syntax & Examples

Array.copyWithin() method

The copyWithin() method of the Array class in JavaScript copies a sequence of array elements within an array to a different position in the same array, without modifying its length.


Syntax of Array.copyWithin()

There are 2 variations for the syntax of Array.copyWithin() method. They are:

1.
copyWithin(target, start)

Parameters

ParameterOptional/RequiredDescription
targetrequiredThe index at which to copy the sequence to.
startrequiredThe index at which to start copying elements from.

This method copies the sequence of array elements from start to the end of the array to the target position. The length of the array is not modified.

Returns value of type Array.

2.
copyWithin(target, start, end)

Parameters

ParameterOptional/RequiredDescription
targetrequiredThe index at which to copy the sequence to.
startrequiredThe index at which to start copying elements from.
endoptionalThe index at which to end copying elements. This is exclusive.

This method copies the sequence of array elements from start to end (not included) to the target position. The length of the array is not modified.

Returns value of type Array.



✐ Examples

1 Using copyWithin() method to copy elements within an array

In JavaScript, we can use the copyWithin() method to copy a sequence of elements within the same array.

For example,

  1. We define an array variable arr with elements [1, 2, 3, 4, 5].
  2. We use the copyWithin() method with target 0 and start 3 to copy the sequence of elements starting from index 3 to the beginning of the array.
  3. The resulting array is modified in place.
  4. We log the array to the console using console.log() method to see the copied elements.

JavaScript Program

const arr = [1, 2, 3, 4, 5];
arr.copyWithin(0, 3);
console.log(arr);

Output

[4, 5, 3, 4, 5]

2 Using copyWithin() method with a specified end index

We can use the copyWithin() method to copy a sequence of elements within the same array, specifying the end index.

For example,

  1. We define an array variable arr with elements ['a', 'b', 'c', 'd', 'e'].
  2. We use the copyWithin() method with target 1, start 3, and end 4 to copy the sequence of elements starting from index 3 to index 1.
  3. The resulting array is modified in place.
  4. We log the array to the console using console.log() method to see the copied elements.

JavaScript Program

const arr = ['a', 'b', 'c', 'd', 'e'];
arr.copyWithin(1, 3, 4);
console.log(arr);

Output

['a', 'd', 'c', 'd', 'e']

3 Using copyWithin() method on an array of numbers

In JavaScript, we can use the copyWithin() method to copy a sequence of numbers within the same array.

For example,

  1. We define an array variable numArr with elements [10, 20, 30, 40, 50].
  2. We use the copyWithin() method with target 2, start 0, and end 2 to copy the first two elements to the position starting from index 2.
  3. The resulting array is modified in place.
  4. We log the array to the console using console.log() method to see the copied elements.

JavaScript Program

const numArr = [10, 20, 30, 40, 50];
numArr.copyWithin(2, 0, 2);
console.log(numArr);

Output

[10, 20, 10, 20, 50]

Summary

In this JavaScript tutorial, we learned about copyWithin() method of Array: the syntax and few working examples with output and detailed explanation for each example.