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)
The push() method takes the item(s) to add to the Array.
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.
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
const songs = ['Dangerous', 'Thriller']; const totalSongs = songs.push('Stranger In Moscow'); console.log(totalSongs); console.log(songs);
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
We have added three elements in a single time, and all the elements are added in the Array.
thanks for the post