Javascript programming articles and tutorials for beginner | Anjan Dutta

Add elements to an array in javascript

Add elements to an array in javascript

Add element at the start of an array

To add an element at the beginning of an array, we can use the unshift() method in javascript.

The unshift() method takes any number of comma-separated parameters. And, all these parameters will be inserted at the beginning of the array.

Here is an example.

var number_list = ["Three", "Four", "Five"];
number_list.unshift("One", "Two");
// ["One", "Two", "Three", "Four", "Five"]

Remember, the order of insertion will be the same as we order the parameters.

Add element at the end of an array

To add an item to the end of an array, we can use the push() function.

The push() function takes any number of parameters, and the order of insertion remains the same.

Here is an example.

var number_list = ["Three", "Four", "Five"];
number_list.push("One", "Two");
// [ "Three", "Four", "Five", "One", "Two"]
Edit this page on GitHub