The push() method adds new items to the end of an array, and returns the new length.
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.
array.push(item1,item2, ...,itemX)
Parameter | Require | Description |
---|---|---|
item1, item2, ..., itemX | Required. | The item(s) to add to the array |
A Number, representing the new length of the array
Add a new item to an array:
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 );