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");
// 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");
// output: ["hamburger", "hot dog", "pizza", "steak", "grilled cheese"]

For other javascript examples: https://www.mikesallese.me/tags/javascript