Mutating Array Methods
Some array methods modify the original array in place — they are called mutating methods. Knowing which methods mutate and which do not is critical for avoiding subtle bugs, especially in frameworks like React where state immutability matters.
push and pop
push(...items) appends items to the end and returns the new length. pop() removes and returns the last item. Both operate in O(1) time.
shift and unshift
shift() removes the first element (returns it). unshift(...items) adds items to the beginning. Both are O(n) because all remaining elements must be re-indexed.
splice(start, deleteCount, ...items)
splice is the Swiss army knife of array mutation — it can remove, replace, and insert elements at any position. It returns an array of the removed elements.
sort and reverse
sort(compareFn) sorts in place. Without a compare function it sorts by string Unicode, which gives wrong results for numbers. reverse() reverses in place.