

For more Javascript, check out my other articles here. Javascript slice copies still have the same reference as the original in memory, which is useful to know when manipulating them. It can also be used in a limited way to make duplicates that can be independently updated. The Javascript slice method is useful for creating new shallow copies of arrays with a subset of the original arrays data. The slice() method selects the elements starting at the given start. Knowing this little piece of trivia is going to save you a lot of time someday. The slice() method returns the selected elements in an array, as a new array object.

That means that although it may seem like you are making a copy with slice when working with simple arrays, this does not hold up when using it on more complex objects. However, somewhat confusingly, by using the arrayOneSlice notation, Javascript interprets this as putting a new value into the place of the shallow copy itself.

In the first example, using the ems notation, Javascript interprets this as updating an existing element within the shallow copy and thus it affects the original. slice returns a sub-set of the original array, with the original array remaining untouched. Wait, what? We changed arrayOneSlice’s items object, but its changed in both arrayOne and arrayOneSlice! Meanwhile, arrayOneSlice has only changed arrayOneSlice! Welcome to another Javascript quirk. An important consideration relating to the answer by insomniac is that splice and slice are two completely different functions, with the main difference being: splice manipulates the original array.
