totype.push() - JavaScript - MDN Web Docs

  • Skip to main content
  • Skip to search
Array.prototype.push() Baseline Widely available

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, …, elementN

The 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); // 4

Merging 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); // 2

Note 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 contribute

This page was last modified on Jul 10, 2025 by MDN contributors.

View this page on GitHub • Report a problem with this content Filter sidebar
  1. Standard built-in objects
  2. Array
  3. Constructor
    1. Array()
  4. Static methods
    1. from()
    2. fromAsync()
    3. isArray()
    4. of()
  5. Static properties
    1. [Symbol.species]
  6. Instance methods
    1. at()
    2. concat()
    3. copyWithin()
    4. entries()
    5. every()
    6. fill()
    7. filter()
    8. find()
    9. findIndex()
    10. findLast()
    11. findLastIndex()
    12. flat()
    13. flatMap()
    14. forEach()
    15. includes()
    16. indexOf()
    17. join()
    18. keys()
    19. lastIndexOf()
    20. map()
    21. pop()
    22. push()
    23. reduce()
    24. reduceRight()
    25. reverse()
    26. shift()
    27. slice()
    28. some()
    29. sort()
    30. splice()
    31. toLocaleString()
    32. toReversed()
    33. toSorted()
    34. toSpliced()
    35. toString()
    36. unshift()
    37. values()
    38. with()
    39. [Symbol.iterator]()
  7. Instance properties
    1. length
    2. [Symbol.unscopables]
  8. Inheritance
  9. Object/Function
  10. Static methods
    1. apply()
    2. bind()
    3. call()
    4. toString()
    5. [Symbol.hasInstance]()
  11. Static properties
    1. displayName Non-standard
    2. length
    3. name
    4. prototype
    5. arguments Non-standard Deprecated
    6. caller Non-standard Deprecated
  12. Instance methods
    1. __defineGetter__() Deprecated
    2. __defineSetter__() Deprecated
    3. __lookupGetter__() Deprecated
    4. __lookupSetter__() Deprecated
    5. hasOwnProperty()
    6. isPrototypeOf()
    7. propertyIsEnumerable()
    8. toLocaleString()
    9. toString()
    10. valueOf()
  13. Instance properties
    1. __proto__ Deprecated
    2. constructor

Tag » Add Element If Not In Array Javascript