Adding an element at the end of the array in Javascript

Front End TechnologyWeb DevelopmentJavascript

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"]
raja
Published on 26-Jun-2018 16:58:59
Advertisements