**push()** mutates the array by appending elements to the end. It returns the new **length** (not the array). Add multiple items in one call: `arr.push(1, 2, 3)`. For non-mutating append, use the spread operator: `[...arr, newItem]`.
1Understanding Array.push()
push() mutates the array by appending elements to the end. It returns the new length (not the array). Add multiple items in one call: arr.push(1, 2, 3). For non-mutating append, use the spread operator: [...arr, newItem].
push() returns the new LENGTH, not the array. To chain, use concat() or spread instead.
const stack = [];
stack.push('first');
stack.push('second', 'third');
console.log(stack); // ['first','second','third']
console.log(stack.length); // 3
// Returns new length
const len = stack.push('fourth');
console.log(len); // 42Practical Example
Here is a real-world application of Array.push() showing how it is used in production JavaScript code.
// Non-mutating alternative
const original = [1, 2, 3];
const newArr = [...original, 4, 5];
console.log(original); // [1, 2, 3] (unchanged)
console.log(newArr); // [1, 2, 3, 4, 5]3Best Practices
Follow these guidelines when working with Array.push():
1. Use spread [...arr, item] for non-mutating append
2. Push multiple items at once: arr.push(a, b, c)
3. Use concat() or spread when building new arrays in functional code
Tip: push() returns the new LENGTH, not the array. To chain, use concat() or spread instead.
const stack = [];
stack.push('first');
stack.push('second', 'third');
console.log(stack); // ['first','second','third']
console.log(stack.length); // 3
// Returns new length
const len = stack.push('fourth');
console.log(len); // 4