Add and remove JavaScript array elements with push, pop, unshift, and shift
Use push() to append to a JavaScript array and unshift() to prepend. Use pop() to take the last value and shift() to take the first. All four mutate the array. The addition methods return its new length, and the removal methods return the value they removed.
Compare positions and return values
| Method | Change to the original array | Returns |
|---|---|---|
push(value) |
Add to the end | New array length |
unshift(value) |
Add to the start | New array length |
pop() |
Remove the last element | Removed value / undefined |
shift() |
Remove the first element | Removed value / undefined |
In const n = items.push('four'), n is a number giving the length. If you need a new array, use array spread to add at either end. Use items.slice(1) for a copy without the first element or items.slice(0, -1) for a copy without the last.
Add elements with push() and unshift()
Run the snippet in the console. push('red') appends to the end and returns 3. One call to unshift('black', 'white') prepends the values in that order and returns 5. Two separate calls that add black and then white instead leave white first. See push() and unshift().
const colors = ['blue', 'green'];
const afterPush = colors.push('red');
console.log(colors); // ['blue', 'green', 'red']
console.log(afterPush); // 3
const afterUnshift = colors.unshift('black', 'white');
console.log(colors);
// ['black', 'white', 'blue', 'green', 'red']
console.log(afterUnshift); // 5
const separate = ['blue'];
separate.unshift('black');
separate.unshift('white');
console.log(separate); // ['white', 'black', 'blue']
Remove elements with pop() and shift()
On an array with values, pop() removes from the end and shift() removes from the start. On an empty array both return undefined and leave the length at 0. Check length beforehand if you need to know whether the array was empty. Removing the value from [undefined] also returns undefined, but it removes an element. See pop() and shift().
const colors = ['blue', 'green', 'red'];
console.log(colors.pop()); // 'red'
console.log(colors); // ['blue', 'green']
console.log(colors.shift()); // 'blue'
console.log(colors); // ['green']
const empty = [];
console.log(empty.pop()); // undefined
console.log(empty.shift()); // undefined
const stored = [undefined];
console.log(stored.length); // 1
console.log(stored.pop()); // undefined
console.log(stored.length); // 0
Complete example: try all four methods on one array
Save the HTML in UTF-8 as array-ends.html and open it in a browser. Each button shows a return value, the current array, and its length. Quotation marks distinguish returned strings from numbers. Reset restores both the input and the array.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Change both ends of an array</title>
</head>
<body>
<label for="value">Value to add</label>
<input id="value" type="text" value="four">
<button id="push" type="button">push: Add to end</button>
<button id="unshift" type="button">unshift: Add to front</button>
<button id="pop" type="button">pop: Remove from end</button>
<button id="shift" type="button">shift: Remove from front</button>
<button id="reset" type="button">Reset</button>
<pre id="output" aria-live="polite"></pre>
<script>
const original = ['one', 'two', 'three'];
let items = original.slice();
const value = document.querySelector('#value');
const output = document.querySelector('#output');
function show(label, returned, didRun = true) {
const rendered = didRun ? (JSON.stringify(returned) ?? 'undefined') : '—';
output.textContent = `return value (${label}): ${rendered}\n` +
`Current array: ${JSON.stringify(items)}\nLength: ${items.length}`;
}
function add(method) {
const text = value.value.trim();
if (text === '') {
output.textContent = "Enter a value to add.";
return;
}
const returned = method === 'push' ? items.push(text) : items.unshift(text);
show(`${method}()`, returned);
value.value = '';
}
document.querySelector('#push').addEventListener('click', () => add('push'));
document.querySelector('#unshift').addEventListener('click', () => add('unshift'));
document.querySelector('#pop').addEventListener('click', () => show('pop()', items.pop()));
document.querySelector('#shift').addEventListener('click', () => show('shift()', items.shift()));
document.querySelector('#reset').addEventListener('click', () => {
items = original.slice();
value.value = 'four';
show("Initial state", undefined, false);
});
show("Initial state", undefined, false);
</script>
</body>
</html>
Append the default input four to get the number 4. Remove from the end immediately afterward to get the string "four" and a length of 3 again. Repeat at the start. Remove all items and try once more to see undefined and length 0.
Combine the methods to make a stack or queue
push() and pop() work on the same end to form a stack, where the last value added comes out first. push() at the end and shift() at the front make a queue, where the first value added comes out first. Add two values in the same order and compare which comes out.
const stack = [];
stack.push('A', 'B');
console.log(stack.pop()); // 'B'
console.log(stack.pop()); // 'A'
const queue = [];
queue.push('A', 'B');
console.log(queue.shift()); // 'A'
console.log(queue.shift()); // 'B'
Use a stack to undo the most recent entry and a queue to process entries in arrival order. Repeatedly removing the first element of a long array can be costly because later elements change indices. If performance matters, measure with realistic inputs and consider a queue that tracks the front position separately.




