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");
What Is JavaScript Used For?
Tutorial
Introduction
Output
Syntax
Comment
Commands
Operators
Comparison and Logical Operators
Data Types
Math.random()
Type Conversion
Function Definitions
Events
Objects
Object Properties
Prototype
Array
Sorting Arrays
Strings
Numbers
Number Format
Math Object
Onclick Event
Date
Date Formats
Scope
Regular Expressions
Reserved Words
Common Mistakes
Performance
Forms
Form Validation
Window: The Browser Object Model
Popup Boxes
Cookies
JSON
AJAX Introduction
AJAX Form
Automatic File Download
Functions
Array Methods
String Methods
Date Methods
Timing Events
Cheat Sheet
JavaScript in HTML
HTML DOM Methods
HTML DOM Changing HTML
HTML DOM Animation
HTML DOM EventListener
HTML DOM Navigation
HTML DOM NodeList
HTML DOM Element Nodes
Array Functions
Boolean
Calling a Function
Date Functions
Global Objects
Input Text
Operator
Statements
String Functions
Math
Math.random
Number
RegEx
alert
array.filter
array.length
array.map
array.reduce
array.push
array.sort
break and continue
className
confirm
decodeURIComponent
for
forEach
if
indexOf
innerHTML
location.reload
number.toString
onclick
onload
parseInt
prompt
replace
setAttribute
setInterval
setTimeout
slice
splice
string.includes
string.indexOf
string.split
style.display
submit
substr
substring
switch
test
throw, try and catch
toLowerCase
toUpperCase
use strict
while
window.history
window.location
window.navigator
window.screen