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.
Udacity
Pros
  • Simplistic design (no unnecessary information)
  • High-quality courses (even the free ones)
  • Variety of features
Main Features
  • Nanodegree programs
  • Suitable for enterprises
  • Paid certificates of completion
Udemy
Pros
  • Easy to navigate
  • No technical issues
  • Seems to care about its users
Main Features
  • Huge variety of courses
  • 30-day refund policy
  • Free certificates of completion
Datacamp
Pros
  • Great user experience
  • Offers quality content
  • Very transparent with their pricing
Main Features
  • Free certificates of completion
  • Focused on data science skills
  • Flexible learning timetable

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");