Syntax
arr.push(item); arr.pop();
arr.unshift(item); arr.shift();Examples
push() and pop() - End of Array
Adding and removing elements from the end.
const stack = [1, 2, 3];
stack.push(4); // adds to the end
console.log(stack); // [1, 2, 3, 4]
const removed = stack.pop(); // removes and returns the last element
console.log(removed); // 4
console.log(stack); // [1, 2, 3]unshift() and shift() - Start of Array
Adding and removing elements from the beginning.
const queue = [2, 3, 4];
queue.unshift(1); // adds to the start
console.log(queue); // [1, 2, 3, 4]
const first = queue.shift(); // removes and returns the first element
console.log(first); // 1
console.log(queue); // [2, 3, 4]Adding Multiple Elements at Once
Each method accepts multiple arguments.
const arr = [3];
arr.push(4, 5, 6);
console.log(arr); // [3, 4, 5, 6]
arr.unshift(1, 2);
console.log(arr); // [1, 2, 3, 4, 5, 6]Best practices
- Prefer push()/pop() over unshift()/shift() when performance matters - operating on the end of an array is significantly faster
- Remember all four methods mutate the original array - use spread syntax ([...arr, newItem]) instead if you need an immutable update
- Use push() to build up an array in a loop rather than repeatedly using concat(), which creates a new array each time
- Check array length before calling pop()/shift() on a potentially empty array, since they return undefined rather than throwing
At a glance
- Purpose
- Application logic and interaction
- File extension
- .js ยท .mjs
- Runs in
- Browsers and JavaScript runtimes
- Usually used with
- HTML, CSS and Web APIs
Specifications & further reading
Related JavaScript documentation
Array Basics
Arrays are ordered, zero-indexed collections that can hold values of any type, including a mix of types. They can be created with array literal syntax [] or the Array constructor. The length property reflects the number of elements, and static methods like Array.isArray(), Array.from(), and Array.of() help create or check arrays.slice() and splice()
slice() returns a shallow copy of a portion of an array as a new array, without modifying the original - useful for extracting a section. splice() modifies the original array in place by removing, replacing, or inserting elements at a specific position, and returns the removed elements.concat() and join()
concat() merges two or more arrays into a new array, without modifying the originals. join() converts all array elements into a single string, separated by a specified delimiter (comma by default) - the reverse operation of String.prototype.split().indexOf(), includes(), lastIndexOf()
These methods search an array for a specific value. indexOf() returns the first matching index (or -1 if not found), lastIndexOf() searches from the end, and includes() simply returns true or false. All use strict equality (===) for comparison, so they cannot find objects by content, only by exact reference.
Arrays are ordered, zero-indexed collections that can hold values of any type, including a mix of types. They can be created with array literal syntax [] or the Array constructor. The length property reflects the number of elements, and static methods like Array.isArray(), Array.from(), and Array.of() help create or check arrays.slice() and splice()
slice() returns a shallow copy of a portion of an array as a new array, without modifying the original - useful for extracting a section. splice() modifies the original array in place by removing, replacing, or inserting elements at a specific position, and returns the removed elements.concat() and join()
concat() merges two or more arrays into a new array, without modifying the originals. join() converts all array elements into a single string, separated by a specified delimiter (comma by default) - the reverse operation of String.prototype.split().indexOf(), includes(), lastIndexOf()
These methods search an array for a specific value. indexOf() returns the first matching index (or -1 if not found), lastIndexOf() searches from the end, and includes() simply returns true or false. All use strict equality (===) for comparison, so they cannot find objects by content, only by exact reference.