Javascript - Array push() Method

The push() method adds new items to the end of an array, and returns the new length.

Description

The push() method adds new items to the end of an array, and returns the new length.

This method changes the length of the array.

To add items at the beginning of an array, use the unshift() method.

push() method is using the array as a stack.

Syntax

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

Parameter Values

Parameter Require Description
item1, item2, ..., itemX Required. The item(s) to add to the array

Return

A Number, representing the new length of the array

Example

Add a new item to an array:

Demo

var myArray = ["XML", "Json", "Database", "Mango"];
console.log( myArray);/*from   www.  j av  a2 s.c om*/

myArray.push("Kiwi");
console.log( myArray);

//The push() method adds new items to the end of an array.

var x = myArray.push("newValue");
console.log(x);
console.log( myArray );

//Add more than one item:

var myArray = ["XML", "Json", "Database", "Mango"];
console.log( myArray );

x = myArray.push("newValue", "Lemon", "PineDatabase");
console.log(x);
console.log( myArray );

Result