JavaScriptArraysBeginner

Array.push()

Adds one or more items to the end of an array (mutates).

Review the syntaxStudy the examplesOpen the coding app
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

itemsany[] · Items to add to the end.

See it in practice

Examples

1

Basic usage with single and multiple arguments

const animals = ['pigs', 'goats'];
const count = animals.push('cows', 'sheep');
console.log(animals);
console.log(count);
Output:
['pigs', 'goats', 'cows', 'sheep'] 4

Adds multiple string elements to the end of the array and captures the returned length.

2

Merging arrays into an existing array

const fruits = ['apple', 'banana'];
const moreFruits = ['orange', 'mango'];
fruits.push(...moreFruits);
console.log(fruits);
Output:
['apple', 'banana', 'orange', 'mango']

Using the ES6 spread operator to unpack elements from one array into the push call of another.

3

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);
Output:
{ '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

1

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]
2

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

Node.js, BrowserAll browsersES3+

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.