Example of pushing to an array in Javascript
By Mike Sallese ·
How to push to a javascript array
const soda = ["coke", "sprite", "fanta"];
const length = soda.push("root beer");
console.log(soda);
// output: ["coke", "sprite", "fanta", "root beer"]
Noteworthy details
pushdoesn’t directly modify the variable, so you can use aconst.pushreturns the updated length of the array.pushcan take more than 1 item at a time:
const food = ["hamburger", "hot dog"];
const length = food.push("pizza", "steak", "grilled cheese");
console.log(food);
// output: ["hamburger", "hot dog", "pizza", "steak", "grilled cheese"]
For other javascript examples: https://www.mikesallese.me/tags/javascript
About the author
Mike Sallese. I build Scribbl, an AI note taker with no bot in the call, and a few other pieces of software. Off the computer I am building a straw bale workshop.
X · Medium · GitHub · articles on other sites