Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

JavaScript Dictionary

  1. Home
  2. JavaScript Dictionary
  3. Array .push() / pop() / shift() / unshift()

Array .push() / pop() / shift() / unshift()

Methods for adding and removing elements at the end or beginning of an array. push() and pop() operate on the end, while unshift() and shift() operate on the beginning.

Syntax

// Adds one or more elements to the end of the array.
array.push(element1, element2, ...);

// Removes and returns the last element of the array.
var removed = array.pop();

// Adds one or more elements to the beginning of the array.
array.unshift(element1, element2, ...);

// Removes and returns the first element of the array.
var removed = array.shift();

Method List

MethodDescription
push(element)Adds one or more elements to the end of the array and returns the new length of the array.
pop()Removes the last element from the array and returns that element. Returns undefined when called on an empty array.
unshift(element)Adds one or more elements to the beginning of the array and returns the new length of the array.
shift()Removes the first element from the array and returns that element. Returns undefined when called on an empty array.

Sample Code

var fruits = ["apple", "orange"];

// Adds an element to the end.
fruits.push("grape");
console.log(fruits); // Outputs "apple,orange,grape".

// Removes the last element.
var last = fruits.pop();
console.log(last); // Outputs "grape".
console.log(fruits); // Outputs "apple,orange".

// Adds an element to the beginning.
fruits.unshift("banana");
console.log(fruits); // Outputs "banana,apple,orange".

// Removes the first element.
var first = fruits.shift();
console.log(first); // Outputs "banana".
console.log(fruits); // Outputs "apple,orange".

Overview

These four methods are the most fundamental ways to manipulate elements at the end and beginning of an array. push() and pop() operate on the end, while unshift() and shift() operate on the beginning. All four are mutating methods that modify the original array directly.

push() can add multiple elements at once by passing them as comma-separated arguments, and its return value is the new length of the array. pop(), on the other hand, takes no arguments and returns the removed element itself. These two methods are commonly used together to implement a stack (last-in, first-out) data structure.

To add or remove elements in the middle of an array, use splice().

Browser Compatibility

Chrome Chrome
49+
Supported in all versions
Firefox Firefox
57+
Supported in all versions
Safari Safari
18+
Supported in all versions
Edge Edge
80+
11 or earlier ×
IE IE
11+
4.5 or earlier ×
Opera Opera
48+
3 or earlier ×
iOS Safari iOS Safari
18+
Supported in all versions
Android Browser Android Browser
37+
Supported in all versions
Chrome Android Chrome Android
36+
17 or earlier ×
Firefox Android Firefox Android
79+
3 or earlier ×

If you find any errors or copyright issues, please .