// 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.