Javascript Add to Array with Array.Prototype.Push() Method

Last Updated on by in JavaScript
Javascript Add to Array Example with Array.Prototype.Push() method is an essential topic for any JS programmer. JavaScript’s offers push() method; it includes a new item into the array and returns a new array with a new length.The push() method includes the item at the end of the array, However, if you want to include a new item at the beginning of the array, then you should use JavaScript’s unshift() method.

Let’s checkout the syntax for pushing the item into the Javascript Array.


JavaScript’s push() method adds the new value into the array. The push() method includes a new item at the end of the array; it changes the original length of the array and returns the newly updated length.

Adding or pushing item to JavaScript array

let movies = ['The Matrix', 'Forest Gump'];

// output: 2

let moviesList = movies.push('Avenger End Game');
// output: 3

// output: ["The Matrix", "Forest Gump", "Avenger End Game"]

JavaScript merging two arrays

Let me show you how you can merge two arrays using apply() method. Now you can see JavaScript’s apply() method is adding the second array into the first array by returning the original array(newMovies), check out the result below.

Here we need to keep in mind that if the second array is large, then we should not use JavaScript apply() method. The apply() method has a limitation for arguments to be passed within the apply() method.

let movies = ['The Matrix', 'Forest Gump'];
let newMovies = ['Infinity War', 'End Game'];

Array.prototype.push.apply(movies, newMovies);

// output: ["The Matrix", "Forest Gump", "Infinity War", "End Game"]

JavaScript adding multiple items in an array

Checkout the following code.

let movies = ['The Matrix', 'Forest Gump'];

movies.push('Infinity War', 'End Game');

// output: ["The Matrix", "Forest Gump", "Infinity War", "End Game"]

As you can see we’ve added two more movies into the array.

let movies = ['The Matrix', 'Forest Gump'];
let newMovies = ['Infinity War', 'End Game']


// output: ["The Matrix", "Forest Gump", Array[2]]

As you can see in the second example we’ve pushed array within the array, so it can’t be combined as we’ve shown you earlier with apply() method.

Javascript add to array example with Array.prototype.push() method is over for now. Thanks a lot checking out my tutorial.

Digamber - Author

Hi, I'm Digamber Singh, a New Delhi-based full-stack developer, tech author, and open-source contributor with 10+ years' experience in HTML, CSS, JavaScript, PHP, and WordPress.