JavaScript Array push() Method

The array push() is a built-in JavaScript function that adds a new element at the end of an array and returns the new length. The syntax of the push() method is an array.push(element), where the element is an item that needs to be added.

Syntax

array.push(element)

You can use a push() method to append more than one value to an array in a single call.

array.push(item1, item2, item3,...,itemN)

Parameters

The push() method takes the item(s) to add to the Array.

Return Value

It returns the number representing the new length of the Array.

The push() method can be used with a call() or apply() on objects resembling the arrays.

The array.push()  method relies on the length property to resolve where to insert the elements.

If the length cannot be converted into a number, the index used is 0.

It includes the possibility of length being nonexistent, in which case it will also be created.

Example 1: Simple program of the array.push() function

The array push() method accepts an element and appends it to the array.

The push() method modifies the length of the Array or collection and can append a number, string, object, array, or any value to the array.

data = ['Python', 'Javascript', 'PHP']
len = data.push('Golang')
console.log('The modified array is: ', data)
console.log('The length of modified array is: ', len)

Output

The modified array is: [ 'Python', 'Javascript', 'PHP', 'Golang' ]
The length of modified array is: 4

First, we defined an array, added a new item called Golang, and stored the new Array length. Then print the mutated Array and its length.

Example 2: How to add elements to an array

Adding elements in the JavaScript array means appending as many elements as possible, like integers, strings, arrays, objects, an array of strings, an array of integers, and an array of arrays.

Let us take the example of Node.js to understand how to add an item in an array in JavaScript.

const songs = ['Dangerous', 'Thriller'];
const totalSongs = songs.push('Stranger In Moscow');

console.log(totalSongs);

console.log(songs);

Output

3
[ 'Dangerous', 'Thriller', 'Stranger In Moscow' ]

In this example, we added the third element, “Stanger in Moscow“. Then, after adding an item to the Array, we printed the mutated Array and its length.

Example 3: How to add multiple elements in the Array

To add multiple elements in the JavaScript array, use the array.push() function. We can add multiple elements as arguments at a single time.

data = ['Python', 'Javascript', 'PHP']
len = data.push('Golang', 'C#', 'Swift')
console.log('The modified array is: ', data)
console.log('The length of modified array is: ', len)

Output

The modified array is: [ 'Python', 'Javascript', 'PHP', 'Golang', 'C#', 'Swift' ]
The length of modified array is: 6

We have added three elements in a single time, and all the elements are added in the Array.

Conclusion

JavaScript Array push() method adds one or more values to the end of an array. It changes the array’s length by the number of elements added and returns the new length of the array.

1 thought on “JavaScript Array push() Method”

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.