// Older way var clonedArray= oldArray.slice(0) // ES6 way: spread operator var clonedArrayES6= [...oldArray]
Equality and sameness: Unlike “=” operator, which creates a new variable which just points to the original array instead of copying its elements, spread operator creates a new, cloned array, with different reference but same values. Hence “=” operator creates a deep copy of the original array but the spread operator does a shallow copy. The array created by the spread operator has the same value as that of the old array but, is not as same as the old array.
Note: All the above examples can be tested by typing them within the script tag of HTML or directly into the browser’s console.
- How to Clone a Map in Java
- Queue.Clone() Method in C#
- PHP | Imagick clone() Function
- PHP | ImagickDraw clone() Function
- How to Clone a List in Java?
- jQuery | clone() with Examples
- Stack.Clone() Method in C#
- Clone a stack without extra space
- ArrayList clone() method in Java with Examples
- kasai’s Algorithm for Construction of LCP array from Suffix Array
- Array.BinarySearch(Array, Object) Method with examples in C#
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.