Example of pushing to the front of an array in Javascript

Simple example of pushing to the front of an array in javascript.

How to push to the front of a javascript array

const soda = ["coke", "sprite", "fanta"];
const length = soda.unshift("root beer");
console.log(soda);
// output: ["root beer", "coke", "sprite", "fanta"]

Noteworthy details

  1. unshift adds one of more items to the beginning of the array.

Here’s an example of pushing multiple items to the front of an array:

const food = ["hamburger", "hot dog"];
const length = food.unshift("pizza", "steak", "grilled cheese");
console.log(food);
// output: ["pizza", "steak", "grilled cheese", "hamburger", "hot dog"]

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