Sept 15, 2022
An array is a special type of variable that doesn’t store a single value; it stores a whole list of values.
let groceryList = [
'Apples',
'Hummus',
'Cheese',
'Chocolate',
];
To reference a single element in an array, use a numeric index reference inside of square brackets:
console.log(groceryList[0]); // outputs "Apples"
Note: indexes start at 0, so the first element is groceryList[0], the second is groceryList[1], etc.
Arrays can be looped over, performing an action on each element:
for (let i = 0; i < groceryList.length; i++) {
console.log(groceryList[i]);
}
for (item of groceryList) {
console.log(item);
}
Additional ways of working with arrays are in Array Methods section.