Master Code
On The Go

Learn. Practice. Build.

Home › Code Snippets › JavaScript › Reference › push()

JavaScript Reference

Array.prototype.push()

Add elements to the end of an array. Returns the new length.

Last updated: October 2026

Definition: Array.prototype.push() adds one or more elements to the end of an array and returns the new length. It mutates the original array in place.

🎬 Live Example

Five push patterns. Click any button to see the output.

Input:

                
Output:

            

💡 push mutates in place and returns the new length, not the array.

📝

Basic Syntax

array.push(element1, element2, ...)

// Returns the new length of the array
// Mutates the original array

🎯 Syntax & Parameters

Parameter Type Description
elementNanyOne or more elements to add. Optional.
1

Add a Single Element

const fruits = ['apple', 'banana'];
fruits.push('cherry');
// fruits: ['apple', 'banana', 'cherry']
2

Add Multiple Elements

const nums = [1, 2];
nums.push(3, 4, 5);
// nums: [1, 2, 3, 4, 5]

Pro tip: Order is preserved — 3, then 4, then 5.

3

Push vs Push Spread

const arr = [1, 2];
const other = [3, 4];

arr.push(other);
// arr: [1, 2, [3, 4]]  ❌ nested array

arr.push(...other);
// arr: [1, 2, 3, 4]     ✅ flattened

Watch out: push(arr) adds the array as a single element. Use spread push(...arr) to merge flat.

4

Return Value Trap

const arr = [1, 2, 3];

const result = arr.push(4);
console.log(result);  // 4  ← the NEW LENGTH
console.log(arr);     // [1, 2, 3, 4]

// ❌ Chainable mistake:
arr.push(5).push(6);  // TypeError: 5.push is not a function
5

Real-World: Building a Todo List

const todos = [];

function addTodo(text) {
    todos.push({
        id: todos.length + 1,
        text,
        done: false
    });
}

addTodo('Buy milk');
addTodo('Walk dog');
// todos: [{id:1,...}, {id:2,...}]

In React/Vue, avoid mutating state directly — use spread: setTodos([...todos, newItem]).

🎯 push vs unshift vs concat vs spread

Method Mutates? Use when
pushYesAdd to end (mutating)
unshiftYesAdd to start (mutating)
concatNoMerge arrays (immutable)
[...a, ...b]NoModern immutable merge

⚠️ Common Mistakes

❓ Frequently Asked Questions

What does Array.push() do in JavaScript?

Array.push() adds one or more elements to the end of an array and returns the new length of the array. It mutates the original array in place.

Does push return the new array?

No. push returns the new length (a number), not the array itself. This is a common source of bugs when chaining methods.

What is the difference between push and concat?

push mutates the original array and adds elements to it. concat returns a new array without modifying the original. Use push for in-place additions, concat for immutable merging.

Can push add multiple elements at once?

Yes. push accepts any number of arguments: arr.push(a, b, c) adds three elements in order and returns the new length.

Can I push an array into another array?

Yes, but it will be added as a single nested element. To merge arrays flat, use spread: arr.push(...otherArray) or use concat.

🎯 What's Next?

← All JavaScript Reference