Array.prototype.push()
Adds one or more elements to the end of an array and returns the new length.
Syntax
arr.push(...elements) Parameters
| Name | Type | Required | Description |
|---|---|---|---|
elements | any | No | One or more elements to append to the end of the array. |
Returns
number — The new length of the array after the push.
Examples
const a = [1, 2];
const len = a.push(3, 4);
console.log(a, len);
Output
[ 1, 2, 3, 4 ] 4
const a = [];
a.push('x');
console.log(a);
Output
[ 'x' ]
Notes
Mutates the array in place and returns the length, not the array. Use the
spread operator for a non-mutating append.
Browser & runtime support
| Environment | Since version |
|---|---|
| chrome | 1.0 |
| firefox | 1.0 |
| safari | 1.0 |
| edge | 12 |
| node | 0.10 |