External Exam Download Resources Web Applications Games Recycle Bin

Arrays

array.js

food = ["sushi","pizza","tacos"];
alert(food[1]);

push.js

food = ["sushi","pizza","tacos"];
food.push("vegetable soup"); //adds to the array
alert("You have " + food.length + " total food(s):\n" + food);

firstOrLast.js

food = ["sushi","pizza","tacos", "vegetable soup"];
    
first = food.shift();
second = food.shift();
last = food.pop();
    
alert(first); //sushi
alert(second); //pizza
alert(last); //vegetable soup
alert(food); //tacos (the only food remaining)

There are many more array methods available in JavaScript that have not been covered here. Try and experiment with some of these methods:

  1. unshifting

  2. splicing

  3. slicing

  4. sorting or reversing - as shown here