Javascript: Make independent copy of array using slice() method

Sometime it happens that we need to make copy of an array but in javascript when we assign an array to new it assign it with reference means if you delete anything from new array it will delete it from the main array as well ­čÖü

To create a copy of an array that is independent of another array:
Use the Array object’s slice() method.

For example, the following statements create an array and then use slice() to make an independent copy of the array.

 // JavaScript syntax

var oldArray = ["1", "b", "3"];

var newArray = oldArray.slice();

This entry was posted in javascript. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *