Javascript · Arrays (Basics)
Creating Arrays
The full text of this chapter, to read or revisit. To practise, do the exercises.
▶ Do the exercisesExercise 1 — One Variable for the Whole Team
You caught three Codémon. With what you know so far, you'd need one variable per Codémon:
let cdm1 = "Aquachu";
let cdm2 = "Pyrochu";
let cdm3 = "Florachu";
console.log(cdm1);
console.log(cdm2);
console.log(cdm3);
That gets messy fast: what about a team of 6, or a bag with 50 items? An array stores several values in a single variable:
let team = ["Aquachu", "Pyrochu", "Florachu"];
console.log(team);
One variable, three values, kept in order. Arrays are everywhere in JavaScript: teams, inventories, scores, lists of moves...
Exercise 2 — Anatomy of an Array
let name = [value1, value2, value3];
1. Square brackets `[` and `]` open and close the array
2. Each value is an element, separated from the next by a comma
3. The elements keep the order you wrote them in
An array can hold any type of value: strings, numbers, booleans...
let levels = [12, 8, 15];
let moves = ["Splash", "Bubble"];
let flags = [true, false, true];
console.log(levels);
console.log(moves);
console.log(flags);
An array can also be completely empty: `let bag = [];`. Useful when you'll fill it later, like an empty bag at the start of an adventure.
Exercise 3 — Spot the Array
Which line creates an array of three strings?
Options
• ```let team = ["Aquachu", "Pyrochu", "Zap"];```
• ```let team = "Aquachu", "Pyrochu", "Zap";```
• ```let team = ("Aquachu", "Pyrochu", "Zap");```
• ```let team = {"Aquachu", "Pyrochu", "Zap"};```
Hint : Remember the anatomy: values separated by commas, wrapped in the right pair of brackets.
Exercise 4 — Three HP Values
Create an array named `hpValues` containing 100, 80 and 65, in that order.
let hpValues ____ ____100, 80, 65____;
console.log(hpValues);
Options: = [ ] ( ) {
Hint : Assigning an array works like assigning any other value: same operator, then the array itself.
Exercise 5 — Mixed Types and typeof
Nothing stops you from mixing types in one array, but it's rarely a good idea: an array is easier to use when all its elements are the same kind of thing.
let mixed = ["Zap", 12, true];
console.log(mixed);
Here's a surprise: `typeof` doesn't say `"array"`. For JavaScript, an array is a special kind of object. To check whether a value really is an array, use `Array.isArray()`:
let team = ["Aquachu", "Pyrochu"];
console.log(typeof team);
console.log(Array.isArray(team));
console.log(Array.isArray("Aquachu"));
Exercise 6 — Quiz: What Type?
What does this code print?
let moves = ["Spark", "Thunder"];
console.log(typeof moves);
Options
• ```"object"```
• ```"array"```
• ```"string"```
• ```"list"```
Hint : typeof has no dedicated answer for arrays. Remember which family they belong to.
Exercise 7 — Arrays and const
You'll often see arrays declared with `const` rather than `let`. Remember: `const` forbids reassigning the variable with `=`:
const team = ["Aquachu", "Pyrochu"];
console.log(team);
// team = ["Zap", "Metachu"]; // Uncomment this line to see the error!
Reassigning the whole array throws an error. But `const` only locks the variable, not what's inside the array: its content can still change, as you'll see in the next chapters. That's why `const` is the usual choice for arrays.
Exercise 8 — Arrays at a Glance
Match each expression with its description.
Items to match
• []
• ["Zap", "Metachu"]
• [3, 7, 9]
• typeof [1, 2]
Definitions
• An empty array
• An array of 2 strings
• An array of 3 numbers
• "object"
Exercise 9 — A Moves Array
Declare `moves` as a constant containing `"Spark"` and `"Thunder"`, then check that it really is an array.
____ moves = ["Spark", "Thunder"];
console.log(Array.isArray(____));
Options: const moves "moves" array
Hint : A constant uses a specific keyword. Then the check needs the variable itself, not its name written as text.
Exercise 10 — Three Strings or One?
Which array contains exactly three elements?
Options
• ```["Aquachu", "Pyrochu", "Zap"]```
• ```["Aquachu, Pyrochu, Zap"]```
• ```["Aquachu Pyrochu Zap"]```
• ```["Aquachu", "Pyrochu, Zap"]```
Hint : Only the commas that sit outside the quotes separate elements.
Exercise 11 — Adventure Kit
Prepare your adventure:
1. Create a constant `team` holding three Codémon names of your choice
2. Create a constant `levels` holding three numbers
3. Create a constant `bag` that is empty for now
4. Print `team` and `levels`
5. Print whether `bag` really is an array (you should see `true`)
Starter code
// Write your code here
Back to the course