Mutator Methods

Prototype Methods: Mutators

Mutator methods change the array in place. Watch their return values: push() gives the new length, splice() the removed elements, and sort() or fill() the very same array object, not a copy.

Mutator methods of Array.prototype
Method Returns Mutates?
push(), unshift() New length Yes
pop(), shift() Removed element Yes
splice(start, count, ...add) Removed elements Yes
fill(), copyWithin() The same array Yes
sort(compareFn?), reverse() The same array Yes
Adding, removing, filling and sorting in placeJavaScriptLive
const a = [1, 2, 3];
console.log(a.push(4, 5), a.pop(), a.unshift(0), a.shift(), a);  // [1, 2, 3, 4]
console.log(a.splice(1, 2, 'x', 'y', 'z'), a);   // remove 2 items at 1, insert 3
console.log(a.fill(0, -2), [1, 2, 3, 4, 5].copyWithin(0, 3));
console.log([10, 9, 1, 100].sort(), [10, 9, 1, 100].sort((m, n) => m - n));

With no compare function, sort() compares elements as strings, so 100 sorts before 9: pass (m, n) => m - n for numbers or (m, n) => m.localeCompare(n) for text. The sort has been guaranteed stable since ES2019. Pitfalls: