JavaScript Array copyWithin() Method

The copyWithin() method copies a section of an array to another position within the same array. It modifies the original array in place, which means it does not create a new array but instead updates the existing array with the copied elements. This method is particularly useful for rearranging or duplicating parts of an array.

Syntax

javascript
array.copyWithin(target, start, end)

Parameters

  • target: The index where the copied elements will be inserted. If target is negative, it is treated as array.length + target, which means it starts from the end of the array.
  • start (optional): The index from which to begin copying. If omitted, it defaults to 0. If start is negative, it is treated as array.length + start.
  • end (optional): The index at which to stop copying (not including end). If omitted, it defaults to the array's length. If end is negative, it is treated as array.length + end.

Return Value

The copyWithin() method returns the modified array with the copied elements in place. The original array is altered by this method.

Example 1: Basic Usage of copyWithin() Method

javascript
const array = [1, 2, 3, 4, 5];
array.copyWithin(0, 3, 4);
console.log(array); // Output: [4, 2, 3, 4, 5]

Explanation:

  • array.copyWithin(0, 3, 4) copies the elements from index 3 to 4 (excluding 4) to index 0. As a result, [4] replaces the element at index 0, so the array becomes [4, 2, 3, 4, 5].

Example 2: Copying Elements with Negative Indices

javascript
const array = [1, 2, 3, 4, 5];
array.copyWithin(-3, -1);
console.log(array); // Output: [1, 2, 5, 5, 5]

Explanation:

  • array.copyWithin(-3, -1) uses negative indices. -3 is treated as 2 (i.e., the third-to-last index), and -1 is treated as 4 (i.e., the last index). The element at index 4 (5) is copied to indices 2, 3, and 4.

Example 3: Copying a Portion of the Array

javascript
const array = [1, 2, 3, 4, 5];
array.copyWithin(2, 0, 3);
console.log(array); // Output: [1, 2, 1, 2, 3]
  • Explanation:

    array.copyWithin(2, 0, 3) copies the elements from index 0 to 3 (excluding 3) to index 2. This results in [1, 2, 1, 2, 3].

Example 4: Using CopyWithin to Overwrite Elements

javascript
const array = [1, 2, 3, 4, 5];
array.copyWithin(1, 3);
console.log(array); // Output: [1, 4, 5, 4, 5]

Explanation:

  • array.copyWithin(1, 3) copies elements from index 3 to the end of the array to index 1. Thus, 4 and 5 overwrite 2 and 3, resulting in [1, 4, 5, 4, 5].

Example 5: Handling Edge Cases

javascript
const array = [1, 2, 3, 4, 5];
array.copyWithin(1, 5);
console.log(array); // Output: [1, 2, 3, 4, 5]

Explanation:

  • array.copyWithin(1, 5) tries to copy elements starting from index 5, which is out of bounds. Since no elements are copied, the array remains unchanged.

Example 6: Copying with Start Greater Than End

javascript
const array = [1, 2, 3, 4, 5];
array.copyWithin(1, 3, 1);
console.log(array); // Output: [1, 2, 3, 4, 5]

Explanation:

  • array.copyWithin(1, 3, 1) has a start index greater than the end index, which means no elements are copied, and the array remains unchanged.

JavaScript

5034

193

Related Articles