Open In App

JavaScript Array copyWithin() Method

Last Updated : 15 Jul, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

The Javascript Array.copyWithin() method considers an array first and then copies part of an array to the same array itself and returns it, without modifying its size but yet the modified data whatever user wishes to have in another’s place i.e, copies array element of an array within the same array.

Syntax: 

copyWithin(target)
copyWithin(target, start)
copyWithin(target, start, end)

Parameters:

This method accepts three parameters as mentioned above and described below:  

  • target: The index position to copy the elements to(Required).
  • start: It is optional. The index position to start copying elements from (default is 0).
  • end: It is optional. The index position to stop copying elements from (default is array.length).

Return value:

  • It returns the modified array.

Example: In this example, we will see the basic use of Array.copyWithin() method for copying one array to another with all three parameters.

// Input array
let array = [1, 2, 3, 4, 5, 6, 7];

// placing at index position 0 the element
// between index 3 and 6
console.log("Array " + array.copyWithin(0, 3, 6));

Output
Array 4,5,6,4,5,6,7

Example 2:  In this example, we will see the basic use of Array.copyWithin() method for copying one array to another with a start index and target value in the parameter.

// Input array
let array = [1, 2, 3, 4, 5, 6, 7];

// Placing from index position 0 the
// Element from index 4
console.log("Array " + array.copyWithin(0, 4));

Output
Array 5,6,7,4,5,6,7

Example 3: In this example, we will see the basic use of Array.copyWithin() method for copying one array to another with only the target value in the parameter.

// Input array
let array = [1, 2, 3, 4, 5, 6, 7];

// Placing from index position 3
// The element from index 0
console.log("Array " + array.copyWithin(3));

Output
Array 1,2,3,1,2,3,4

Example 4: Whenever we need to copy the content of any array to the same array that time we use Array.copyWithin() element in JavaScript.

// Input array
let array = [1, 2, 3, 4, 5, 6, 7];

// Placing at index position 0 the
// Element between index 4 and 5
console.log("Array " + array.copyWithin(0, 4, 5));

Output
Array 5,2,3,4,5,6,7

We have a complete list of Javascript Array methods, to check those please go through this Javascript Array Complete reference article.

Supported Browsers:

The browsers supported by JavaScript Array copyWithin() method are listed below: 

  • Google Chrome 45.0
  • Microsoft Edge 12.0
  • Mozilla Firefox 32.0
  • Opera 32.0
  • Safari 9

JavaScript Array copyWithin() Method- FAQs

What does copyWithin() do in JavaScript arrays?

copyWithin() copies a sequence of elements within the array to another position, overwriting existing elements.

Does copyWithin() modify the original array?

Yes, copyWithin() modifies the original array in place without creating a new array.

How does copyWithin() handle negative indices?

Negative indices are counted from the end of the array, similar to other array methods.

Can copyWithin() be used with arrays of objects?

Yes, copyWithin() works with arrays containing objects, copying references to objects.

Does copyWithin() change the length of the array?

No, copyWithin() preserves the length of the array, only copying existing elements.

We have a Cheat Sheet on Javascript where we covered all the important topics of Javascript to check those please go through Javascript Cheat Sheet-A Basic guide to JavaScript.



Next Article

Similar Reads

three90RightbarBannerImg