Javascript · Arrays (Basics)
.includes() and .indexOf()
The full text of this chapter, to read or revisit. To practise, do the exercises.
▶ Do the exercises
Exercise 1 — Is It in There? includes
To know whether an array contains a value, use `includes()`. It answers with a boolean: `true` or `false`.
const team = ["Aquachu", "Pyrochu", "Florachu"]; console.log(team.includes("Pyrochu")); console.log(team.includes("Zap"));
Since the result is a boolean, it fits directly in a condition:
const team = ["Aquachu", "Pyrochu", "Florachu"]; if (team.includes("Zap")) { console.log("Zap is ready to fight!"); } else { console.log("Zap is not in the team"); }
Exercise 2 — Where Is It? indexOf
`includes` says yes or no. `indexOf()` goes further: it gives the index where the value was found.
const team = ["Aquachu", "Pyrochu", "Florachu"]; console.log(team.indexOf("Florachu")); console.log(team.indexOf("Aquachu"));
If the value isn't in the array, `indexOf` returns `-1`. Not `undefined`, not `0`: `-1`, a value that can never be a real index.
const team = ["Aquachu", "Pyrochu", "Florachu"]; console.log(team.indexOf("Zap"));
Exercise 3 — Quiz: Yes or No
What does this code print?
const moves = ["Spark", "Thunder"]; console.log(moves.includes("Charge"));
Options
• ```false```
• ```true```
• ```-1```
• ```undefined```
Hint : This method only ever answers with one of two values.
Exercise 4 — Ready to Fight?
Print `Pyrochu is ready!` only if Pyrochu is part of the team.
const team = ["Aquachu", "Pyrochu", "Florachu"]; if (team.____("Pyrochu")) { console.log("Pyrochu is ready!"); }
Options: includes indexOf has contains
Hint : You need a method whose answer works directly as a condition.
Exercise 5 — Exact Match Only
Both methods look for an exact match. Uppercase and lowercase matter, and so does every single character:
const team = ["Aquachu", "Pyrochu"]; console.log(team.includes("aquachu")); console.log(team.includes("Aquachu ")); console.log(team.includes("Aquachu"));
Same for numbers: the number `5` and the string `"5"` are two different values.
const levels = [5, 8, 12]; console.log(levels.includes(5)); console.log(levels.includes("5"));
Exercise 6 — Quiz: Position Found
What does this code print?
const team = ["Rockachu", "Zap", "Metachu"]; console.log(team.indexOf("Metachu"));
Options
• ```2```
• ```3```
• ```true```
• ```-1```
Hint : The answer is a position, counted the way arrays always count.
Exercise 7 — Unknown Move
Find the position of the chosen move. If it isn't known, print `Unknown move`, otherwise print its position.
const moves = ["Spark", "Thunder", "Charge"]; let chosen = "Bubble"; let pos = moves.____(chosen); if (pos ____ -1) { console.log("Unknown move"); } else { console.log("Found at " + pos); }
Options: indexOf includes === !== =
Hint : You need a position, and a special value tells you when nothing was found.
Exercise 8 — includes vs indexOf
Match each expression with its result.
Items to match
• includes(x)
• indexOf(x)
• indexOf(x) when x is missing
• ["Zap"].includes("zap")
Definitions
• true or false
• The index of x
• -1
• false
Exercise 9 — Quiz: Which Method?
You want to know at which position `"Elixir"` sits in `bag`. Which line do you need?
Options
• ```bag.indexOf("Elixir")```
• ```bag.includes("Elixir")```
• ```bag["Elixir"]```
• ```bag.length("Elixir")```
Hint : One method answers a yes/no question, the other answers a where question.
Exercise 10 — Move Check
Zap wants to use the moves in the variable`chosen`. 1. If the move is in `moves`, print `"Zap uses X! (move number N)"` where N is its position counted from 1 2. Otherwise, print `"Zap doesn't know X"` Test your code with a move that exists and one that doesn't.
Starter code
const moves = ["Spark", "Thunder", "Charge"]; let chosen = "Thunder"; // Write your code here
Back to the course