Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

JavaScript push() method

JavaScript push() method

Array Object Reference JavaScript Array object

Example

Add new elements to an array:

var fruits = ["Banana", "Orange", "Apple", "Mango"];

fruits.push("Kiwi")

fruits Result output:

Banana,Orange,Apple,Mango,Kiwi

Try it »


Definition and Usage

The push() method adds one or more elements to the end of an array and returns the new length.

Note: The new elements will be added to the end of the array.

Note: This method changes the length of the array.

Tip: To add elements at the beginning of an array, use unshift() method.


Browser support

Internet ExplorerFirefoxOperaGoogle ChromeSafari

All major browsers support push().


Syntax

array

.push(

item1

,

item2

, ...,

itemX

)

Parameter value

Parameters Description
item1, item2, ..., itemX Required. The element(s) to add to the array.

Return Value

Type Description
Number New array length

Technical Details

JavaScript version: 1.2

More examples

Example

Add more than one element

var fruits = ["Banana", "Orange", "Apple", "Mango"];

fruits.push("Kiwi","Lemon","Pineapple")

The above example will output

Banana,Orange,Apple,Mango,Kiwi,Lemon,Pineapple

Try it »


Array Object Reference JavaScript Array object