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.

Table of Contents

  1. JavaScript array push() syntax
  2. Adding or pushing item to JavaScript array
  3. JavaScript merging two arrays
  4. JavaScript adding multiple items in an array

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

array.push(item)

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'];

console.log(movies.length);
// output: 2

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


console.log(movies);
// 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);

console.log(movies);
// 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');

console.log(movies); 
// 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']

movies.push(newMovies)

console.log(movies)
// 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.