.reviewed
If an item is mutable, modifying the copy also modifies the original. If it’s immutable, modifying the copy does not affect the original. It’s confusing because immutable sounds like the item can’t be changed. What it actually means, though, is that the original is not changed when the copy is.
array.splice() mutates the original array, (changes the contents of an array) array.slice() does not mutate the original array (returns a shallow copy)
Mutating: 'array.push()
and array.unshift()
Non-mutating: array.concat()
.
Mutating: array.pop()
and array.shift()
and array.splice()
- it also returns the items it removed
Non-Mutating: array.filter()
, array.slice()
Mutating: If you know the index of the item you want to replace, you can use array.splice()
non-Mutating:array.map()
Src and with more examples: https://lorenstewart.me/2017/01/22/javascript-array-methods-mutating-vs-non-mutating/