How to Push an Element into an Array in JavaScript?

Last updated on February 14, 2023

The push() method pushes new elements into the array in JavaScript. It accepts multiple comma-separated values in parameters for pushing them into the array.

Let’s create a variable with a const keyword and assign it an array that contains favorite countries.

const favoriteCountries = ["Germany", "France"];

It has only two countries, for adding more we can attach the push() method to the variable along with the value that needs to be added.

favoriteCountries.push("Vietnam");

Here we passed the Vietnam country into the push() method which will be added to the favorite countries array. To see the result we can use the console.log() method that prints the array in the console.

Output: ['Germany', 'France', 'Vietnam']


Written by
I am a skilled full-stack developer with extensive experience in creating and deploying large and small-scale applications. My expertise spans front-end and back-end technologies, along with database management and server-side programming.

Share on:

Related Posts