Array.prototype.push()
BaselineWidely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
Thepush()
method ofArray
instances adds the specified elements to the end ofan array and returns the new length of the array.
Try it
const animals = ["pigs", "goats", "sheep"];const count = animals.push("cows");console.log(count);// Expected output: 4console.log(animals);// Expected output: Array ["pigs", "goats", "sheep", "cows"]animals.push("chickens", "cats", "dogs");console.log(animals);// Expected output: Array ["pigs", "goats", "sheep", "cows", "chickens", "cats", "dogs"]
Syntax
push()push(element1)push(element1, element2)push(element1, element2, /* …, */ elementN)
Parameters
element1
, …,elementN
The element(s) to add to the end of the array.
Return value
The newlength
property of the object upon which the method was called.
Description
Thepush()
method appends values to an array.
Array.prototype.unshift()
has similar behavior topush()
, but applied to the start of an array.
Thepush()
method is amutating method. It changes the length and the content ofthis
. In case you want the value ofthis
to be the same, but return a new array with elements appended to the end, you can usearr.concat([element0, element1, /* ... ,*/ elementN])
instead. Notice that the elements are wrapped in an extra array — otherwise, if the element is an array itself, it would be spread instead of pushed as a single element due to the behavior ofconcat()
.
Thepush()
method isgeneric. It only expects thethis
value to have alength
property and integer-keyed properties. Although strings are also array-like, this method is not suitable to be applied on them, as strings are immutable.
Examples
Adding elements to an array
The following code creates thesports
array containing two elements, thenappends two elements to it. Thetotal
variable contains the new length ofthe array.
const sports = ["soccer", "baseball"];const total = sports.push("football", "swimming");console.log(sports); // ['soccer', 'baseball', 'football', 'swimming']console.log(total); // 4
Merging two arrays
This example usesspread syntax to push all elements from asecond array into the first one.
const vegetables = ["parsnip", "potato"];const moreVegs = ["celery", "beetroot"];// Merge the second array into the first onevegetables.push(...moreVegs);console.log(vegetables); // ['parsnip', 'potato', 'celery', 'beetroot']
Merging two arrays can also be done with theconcat()
method.
Calling push() on non-array objects
Thepush()
method reads thelength
property ofthis
. It then sets each index ofthis
starting atlength
with the arguments passed topush()
. Finally, it sets thelength
to the previous length plus the number of pushed elements.
const arrayLike = { length: 3, unrelated: "foo", 2: 4,};Array.prototype.push.call(arrayLike, 1, 2);console.log(arrayLike);// { '2': 4, '3': 1, '4': 2, length: 5, unrelated: 'foo' }const plainObj = {};// There's no length property, so the length is 0Array.prototype.push.call(plainObj, 1, 2);console.log(plainObj);// { '0': 1, '1': 2, length: 2 }
Using an object in an array-like fashion
As mentioned above,push
is intentionally generic, and we can use that toour advantage.Array.prototype.push
can work on an object just fine, asthis example shows.
Note that we don't create an array to store a collection of objects. Instead, we storethe collection on the object itself and usecall
onArray.prototype.push
to trick the method into thinking we are dealing withan array—and it just works, thanks to the way JavaScript allows us to establish theexecution context in any way we want.
const obj = { length: 0, addElem(elem) { // obj.length is automatically incremented // every time an element is added. [].push.call(this, elem); },};// Let's add some empty objects just to illustrate.obj.addElem({});obj.addElem({});console.log(obj.length); // 2
Note that althoughobj
is not an array, the methodpush
successfully incrementedobj
'slength
property just like if wewere dealing with an actual array.
Specifications
Specification |
---|
ECMAScript® 2026 Language Specification # sec-array.prototype.push |