totype.push() - JavaScript - MDN Web Docs
Maybe your like
- Skip to main content
- Skip to search
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
- Learn more
- See full compatibility
- Report feedback
The push() method of Array instances adds the specified elements to the end of an array and returns the new length of the array.
In this article
- Try it
- Syntax
- Description
- Examples
- Specifications
- Browser compatibility
- See also
Try it
const animals = ["pigs", "goats", "sheep"]; const count = animals.push("cows"); console.log(count); // Expected output: 4 console.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
jspush() push(element1) push(element1, element2) push(element1, element2, /* …, */ elementN)Parameters
element1, …, elementNThe element(s) to add to the end of the array.
Return value
The new length property of the object upon which the method was called.
Description
The push() method appends values to an array.
Array.prototype.unshift() has similar behavior to push(), but applied to the start of an array.
The push() method is a mutating method. It changes the length and the content of this. In case you want the value of this to be the same, but return a new array with elements appended to the end, you can use arr.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 of concat().
The push() method is generic. It only expects the this value to have a length 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 the sports array containing two elements, then appends two elements to it. The total variable contains the new length of the array.
jsconst sports = ["soccer", "baseball"]; const total = sports.push("football", "swimming"); console.log(sports); // ['soccer', 'baseball', 'football', 'swimming'] console.log(total); // 4Merging two arrays
This example uses spread syntax to push all elements from a second array into the first one.
jsconst vegetables = ["parsnip", "potato"]; const moreVegs = ["celery", "beetroot"]; // Merge the second array into the first one vegetables.push(...moreVegs); console.log(vegetables); // ['parsnip', 'potato', 'celery', 'beetroot']Merging two arrays can also be done with the concat() method.
Calling push() on non-array objects
The push() method reads the length property of this. It then sets each index of this starting at length with the arguments passed to push(). Finally, it sets the length to the previous length plus the number of pushed elements.
jsconst 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 0 Array.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 to our advantage. Array.prototype.push can work on an object just fine, as this example shows.
Note that we don't create an array to store a collection of objects. Instead, we store the collection on the object itself and use call on Array.prototype.push to trick the method into thinking we are dealing with an array—and it just works, thanks to the way JavaScript allows us to establish the execution context in any way we want.
jsconst 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); // 2Note that although obj is not an array, the method push successfully incremented obj's length property just like if we were dealing with an actual array.
Specifications
| Specification |
|---|
| ECMAScript® 2026 Language Specification# sec-array.prototype.push |
Browser compatibility
See also
- Polyfill of Array.prototype.push in core-js with fixes of this method
- es-shims polyfill of Array.prototype.push
- Indexed collections guide
- Array
- Array.prototype.pop()
- Array.prototype.shift()
- Array.prototype.unshift()
- Array.prototype.concat()
- Array.prototype.splice()
Help improve MDN
Was this page helpful to you? Yes No Learn how to contributeThis page was last modified on Jul 10, 2025 by MDN contributors.
View this page on GitHub • Report a problem with this content Filter sidebar- Standard built-in objects
- Array
- Constructor
- Array()
- Static methods
- from()
- fromAsync()
- isArray()
- of()
- Static properties
- [Symbol.species]
- Instance methods
- at()
- concat()
- copyWithin()
- entries()
- every()
- fill()
- filter()
- find()
- findIndex()
- findLast()
- findLastIndex()
- flat()
- flatMap()
- forEach()
- includes()
- indexOf()
- join()
- keys()
- lastIndexOf()
- map()
- pop()
- push()
- reduce()
- reduceRight()
- reverse()
- shift()
- slice()
- some()
- sort()
- splice()
- toLocaleString()
- toReversed()
- toSorted()
- toSpliced()
- toString()
- unshift()
- values()
- with()
- [Symbol.iterator]()
- Instance properties
- length
- [Symbol.unscopables]
- Inheritance
- Object/Function
- Static methods
- apply()
- bind()
- call()
- toString()
- [Symbol.hasInstance]()
- Static properties
- displayName Non-standard
- length
- name
- prototype
- arguments Non-standard Deprecated
- caller Non-standard Deprecated
- Instance methods
- __defineGetter__() Deprecated
- __defineSetter__() Deprecated
- __lookupGetter__() Deprecated
- __lookupSetter__() Deprecated
- hasOwnProperty()
- isPrototypeOf()
- propertyIsEnumerable()
- toLocaleString()
- toString()
- valueOf()
- Instance properties
- __proto__ Deprecated
- constructor
Tag » Add Element If Not In Array Javascript
-
Array.push() If Does Not Exist? - Stack Overflow
-
Array.push() Element If Does Not Exist Using JavaScript | Bobbyhadz
-
Js Add To Array If Not Exists Code Example
-
Push To Array If Not Exist Javascript Code Example - Code Grepper
-
5 Way To Append Item To Array In JavaScript
-
How To Check If Array Includes A Value In JavaScript? - Samantha Ming
-
How To Check If A Value Exists In An Array In JavaScript
-
Array - JavaScript - MDN Web Docs
-
Check If An Item Is In An Array In JavaScript - FreeCodeCamp
-
JavaScript Array Includes() Method - W3Schools
-
Array Methods - The Modern JavaScript Tutorial
-
Add An Item To The Beginning Of An Array In JavaScript Or Node.js
-
Check If The Value Exists In Array In Javascript - Javatpoint
-
Javascript Array Push: How To Add Element In Array - AppDividend
-
How To Check If Key Exists In JavaScript Object/Array - Stack Abuse
-
How To Add Elements Into An Array In JavaScript - Linux Hint
-
How To Check If Value Exists In An Array Using Javascript? - Flexiple
-
JavaScript Arrays - Tips, Tricks And Examples - CodinGame