Example of pushing to an array in Javascript

Simple example of pushing to an array in javascript.

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

  1. push doesn’t directly modify the variable, so you can use a const.
  2. push returns the updated length of the array.
  3. push can 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