The push method

The push() method is used to add elements to the end of an array.
The parameters sent to the method are the new element(s) you wish to add to the array.

Note:
The value returned by the push() method in older browsers is the last element that has been added to the array. In newer browsers is the numeric value of the new length of the array.

var Fruits = new Array("bananas" , "apples");
Fruits.push("grapes");

The code adds a new element to the array using the push() method. Now, the array contains three elements.

You can assign the result to a variable and it will return the numerical value.

var Fruits = new Array("bananas" , "apples");
Fruits.push("grapes");
var MoreFruits = Fruits.push("pears", "mangoes");
document.write("It returns "+MoreFruits+" fruits");

The output of the code will be “It returns 5 fruits”.

Leave a Reply