AppDividend
Latest Code Tutorials

Javascript Array Push Example | Array.prototype.push() Tutorial

506

Get real time updates directly on you device, subscribe now.

Javascript Array Push Example | Array.prototype.push() Tutorial is today’s leading topic.  The push() method adds a new item to the end of an array and returns the new length of an array. The push() method changes the length of the array or collection. If you want to add items at the beginning of an array, then use the javascript unshift() method.

If you want to learn more about the latest Javascript tutorials then check out this The Complete Javascript Course 2018.

Javascript Array Push Example

Let us see the syntax of the Javascript Array push() method.

arr.push(element1)

The push() method appends the values to an array.

The push() method is intentionally generic. This method can be used with a call() or apply() on objects resembling the arrays. The push()  method relies on the length property to resolve to where to start inserting the given items. If the length cannot be converted into the number, the index used is 0. It includes the possibility of length being nonexistent, in which case the length will also be created.

#Adding elements to an array

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

// server.js

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

console.log(totalSongs);

console.log(songs);

 

Javascript Array Push Example | Array.prototype.push() Tutorial

#Merging two arrays

Let us take an example of how we can merge two arrays.

Earn a Tech Degree and get the skills like Frontend Development or Javascript Development that can help you to launch a career. Start your free trial
// server.js

const artists = ['michael', 'elvis'];
const newArtists = ['justin', 'charlie'];

Array.prototype.push.apply(artists, newArtists);

console.log(artists);

 

Array push() example tutorial

Do not use the apply() method if the second array (newArtists in the example) is vast, because the maximum number of arguments that one function can handle is limited in practice.

So, apply() will add the second array into the first array and we can see the combined array by returning the original array in our case it is artists.

#Add Multiple Items in an Array

See the following code.

// server.js

const artists = ['michael', 'elvis'];

artists.push('justin', 'charlie');

console.log(artists); // ['michael', 'elvis', 'justin', 'charlie']

Here, we have added two more artists. Now, see the following code.

// server.js

const artists = ['michael', 'elvis'];
const newArtists = ['justin', 'charlie'];
artists.push(newArtists);

console.log(artists);

 

Javascript array push method

Earn a Tech Degree and get the skills like Frontend Development or Javascript Development that can help you to launch a career. Start your free trial

Here, the third element is an array. So it has added the third item as a whole array. So we can not combine it as we have done it with apply().

Related Posts
1 of 13

Finally, Javascript Array Push Example | Array.prototype.push() Tutorial is over. Thanks for taking.

Leave A Reply

Your email address will not be published.

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