javascript · level 1 · unit 6 · lesson 1
- meet it
- guess first
- your turn
- fix it
- nailed it
Tap a green word if you get lost.
meet it
An keeps many values together in one box.
A shopping list does not need a new piece of paper for every thing you buy. One list holds them all, in order. A JavaScript array does the same, so one variable can hold lots of values.
Write an array with square brackets [ ] and put a comma between the values. Each value in the array is called an . .length counts the items, not the letters.
Showing an array with console.log shows the square brackets and commas too, with no spaces. Text items keep their double quote marks, like ["bread","milk"].
let shopping = ["bread", "milk", "eggs"];
console.log(shopping);
console.log(shopping.length); Arrays can hold numbers too, like [3, 7, 10].
guess first
Three pets. What will this show?
Guess before you run it. Wrong guesses are how you learn, and nothing is marked.
let pets = ["cat", "dog", "goldfish"];
console.log(pets.length);
your turn
Your turn. Make an array of favourites.
Type three of your favourite things, one between each pair of quotes. Then press run to see the array and how many items it has.
fix it
The snack array will not run. Can you fix it?
- It is a SyntaxError on line 1. Look at what sits between each item.
- Every item needs a comma between it and the next one. One comma is missing.
- Add a comma after "apple", then press run.
nailed it
you packed lots of values into one box. :D
- An array holds many items in square brackets.
- Put a comma between every item.
.lengthcounts the items in an array.
new words in your collection
- array
- item
This lesson isn’t saved. A free account remembers every lesson you finish.