JavaScript Array push()

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

The syntax of the push() method is:

arr.push(element1, element2, ..., elementN)

Here, arr is an array.


push() Parameters

The push() method takes in an arbitrary number of elements to add to the array.


Return value from push()

  • Returns the new (after appending the arguments) length of the array upon which the method was called.

Notes:

  • This method changes the original array and its length.
  • To add elements to the beginning of an array, use the JavaScript Array unshift() method.

Example: Using push() method

var languages = ["JavaScript", "Python", "Java", "Lua"];

var count = languages.push("C++");
console.log(languages); // [ 'JavaScript', 'Python', 'Java', 'Lua', 'C++' ]
console.log(count); // 5

var priceList = [12, 21, 35];

var count1 = priceList.push(44, 10, 1.6);
console.log(priceList); // [ 12, 21, 35, 44, 10, 1.6 ]
console.log(count1); // 6

Output

[ 'JavaScript', 'Python', 'Java', 'Lua', 'C++' ]
5
[ 12, 21, 35, 44, 10, 1.6 ]
6

Recommended Readings: