It’s Festive Season! Enjoy 30% Off

Use code FESTIVE30 at checkout

Code has been added to clipboard!

array.push

Reading time 2 min
Published Aug 10, 2017
Updated Oct 2, 2019

JavaScript Array Push: Main Tips

  • JavaScript array push() method is used to add elements to an array and declare a new length.
  • The new items are added to the end of the variable.

push() Explained

JavaScript array push() is a function used to incorporate new HTML elements into an array. By default, the JavaScript add to array method will include the new items at the end of the array.

After the JavaScript push() function is applied to an array, it will deliver a very specific return value. Since the length of the array is modified after the application of JavaScript array push, the method will automatically inform you of the new length of the list of elements.

Tip: while unshift() method has a similar function, it adds items to the beginning of the array.

Syntax and Usage

The syntax rules for this method are very simple. See it for yourself:

array.push(item1, item2, ...)

As you can see in the syntax of JavaScript array push, you only need a couple of components to make JavaScript append to array. First of all, you need to indicate which array is going to be modified with new elements. Secondly, you need to specify the items you want to add to the end of the list.

The following example illustrates how a new element (Rover) is to be added to the cars array. Analyze the full code by clicking the Try it Live button:

Example
var cars = ["Audi", "BWM", "Mercedes", "Porsche"];
cars.push("Rover");