Array.push()
Adds one or more items to the end of an array (mutates).
array.push(...items)This static page keeps the syntax and examples indexed for search, while the coding app handles interactive exploration and saved references.
What it does
Overview
Adds one or more items to the end of an array (mutates).
The push() method appends one or more elements to the end of an array and returns the new length of the array. It is a mutating method, meaning it modifies the original array in place rather than returning a new instance. Performance-wise, push() is generally O(1) (amortized constant time) because it only requires updating the end of the data structure and incrementing the length property. While it is highly efficient for single additions, pushing very large sets of items using spread syntax (e.g., arr.push(...largeArray)) can potentially exceed the maximum call stack size in certain environments because arguments are passed to the stack. Additionally, push() is intentionality generic; it can be used on array-like objects using Function.prototype.call() or apply(), provided the object has a length property and integer-keyed properties.
Quick reference
Syntax
array.push(...items)
Inputs
Parameters
See it in practice
Examples
Basic usage with single and multiple arguments
const animals = ['pigs', 'goats'];
const count = animals.push('cows', 'sheep');
console.log(animals);
console.log(count);['pigs', 'goats', 'cows', 'sheep'] 4
Adds multiple string elements to the end of the array and captures the returned length.
Merging arrays into an existing array
const fruits = ['apple', 'banana'];
const moreFruits = ['orange', 'mango'];
fruits.push(...moreFruits);
console.log(fruits);['apple', 'banana', 'orange', 'mango']
Using the ES6 spread operator to unpack elements from one array into the push call of another.
Applying push to array-like objects
const obj = {
length: 0,
add: function(elem) {
Array.prototype.push.call(this, elem);
}
};
obj.add('first');
obj.add('second');
console.log(obj);{ '0': 'first', '1': 'second', length: 2, add: [Function] }
The push method is generic and can update objects that simulate array structures.
Debug faster
Common Errors
LogicError
Cause: Confusing the return value (new length) with the modified array.
Fix: Remember that push() returns a number, not the array itself. Do not chain it directly if you need the array.
const arr = [1, 2];
const result = arr.push(3);
// result is 3, not [1, 2, 3]LogicError
Cause: Pushing an entire array instead of its individual elements.
Fix: Use the spread operator (...) if you want to flatten the items being added into the target array.
const a = [1, 2];
const b = [3, 4];
a.push(b);
// a is now [1, 2, [3, 4]] instead of [1, 2, 3, 4]Runtime support
Compatibility
Source: MDN Web Docs
Common questions
Frequently Asked Questions
Adds one or more items to the end of an array (mutates).
items: Items to add to the end.
LogicError: Remember that push() returns a number, not the array itself. Do not chain it directly if you need the array. LogicError: Use the spread operator (...) if you want to flatten the items being added into the target array.