// 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
- PHP | ImagickDraw clone() Function
- PHP | Imagick clone() Function
- Underscore.js | _.clone() Function
- jQuery | clone() with Examples
- Lodash | _.clone() Method
- How to Clone a List in Java?
- Queue.Clone() Method in C#
- Stack.Clone() Method in C#
- Clone a stack without extra space
- ArrayList clone() method in Java with Examples
- Array.BinarySearch(Array, Object) Method with examples in C#
- kasai’s Algorithm for Construction of LCP array from Suffix Array
- C# | Array.BinarySearch(Array, Object, IComparer) Method
- Jagged Array or Array of Arrays in C with Examples
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.