Introduction to Arrays
In the lesson on conditions, you taught your programs to make decisions. This lesson is about storing many values together.
Imagine you want to keep the names of five students. With what you know so far, you would need five variables:
const student1 = "Jennifer";
const student2 = "Boris";
const student3 = "Nancy";
const student4 = "David";
const student5 = "Ava";That's a lot of typing. And what if a sixth student joins the class? Every new student would need a new variable. This is what arrays are for. An array is a list of values stored in one variable:
const students = ["Jennifer", "Boris", "Nancy", "David", "Ava"];The values in an array are called its elements. This array has five elements, and they keep their order: "Jennifer" is the first, "Ava" the last.
You print an array with console.log(), like any other value. The console shows it the way it is written in code: in square brackets, with text elements in quotes, so that you can see where each element starts and ends:
const fruits = ["apple", "banana", "cherry"];
console.log(fruits);This prints:
["apple", "banana", "cherry"]Instructions
Print the array students: on the next line, write console.log(students);. The console should show ["Jennifer", "Boris", "Nancy", "David", "Ava"].