Computer >> Computer tutorials >  >> Programming >> Javascript

Adding an element at the end of the array in Javascript


This can be accomplished using the push method. For example,

let veggies = ["Onion", "Raddish"];
veggies.push("Cabbage");
console.log(veggies);

This will give the output −

["Onion", "Raddish", "Cabbage"]

You can also use this to push multiple items at the same time as it supports a variable number of arguments. For example, 

let veggies = ["Onion", "Raddish"];
veggies.push("Cabbage", "Carrot", "Broccoli");
console.log(veggies);

This will give the output −

["Onion", "Raddish", "Cabbage", "Carrot", "Broccoli"]