Array.prototype.push
Appends elements to the end of an array.
Available since: ES3
Syntax
array.push(element1, element2, /* … */)Parameters
| Parameter | Type | Description |
|---|---|---|
| element1, … | any | The elements to append. |
Return value
numberThe new length of the array.
Examples
Appending an element:
const colors = ['red'];
colors.push('green');
console.log(colors.join(', '));Output:
red, greenThe companion pop() removes the last element and returns it:
const stack = ['a', 'b', 'c'];
const last = stack.pop();
console.log(last);
console.log(stack.join(', '));Output:
c
a, bNotes
The method modifies the array itself. unshift() (add) and shift() (remove) work with the start of the array.
See also
- Array.prototype.slice — Returns part of an array.
- Array.prototype.join — Joins array elements into a string.
- Array.prototype.pop — Removes the last element of an array and returns it.
- Array.prototype.unshift — Adds elements to the beginning of an array.
- Spread syntax — Three dots spread an array or object into separate items.
Related recipes
In the tutorial
- Arrays — What an array is in JavaScript: creating one, indexes, length, changing elements and nested arrays.