← ACARA Digital Technologies Years 9–10

Searching an array with includes and indexOf

Array method that gives true if the value is in the array, otherwise false. Its partner indexOf(value) gives the position, or -1 if the value is not found.

Understanding Searching an array with includes and indexOf in ACARA Digital Technologies Years 9–10

What it does: includes gives true or false, so it can be used directly as the if condition. indexOf gives -1 when the value is missing, so comparing with -1 tells you it was not found. -1 means not found. 0 would mean the value is the first item in the array. indexOf() returns the index of the first matching item. If there is no match it returns -1.

Common mistakes: indexOf gives -1 when the value is not found, not 0 (0 is the first position). The check should be i === -1. The method is called includes, with an s on the end. include is not an array method and causes a TypeError.

Class: _____________________  ·  Name: _____________________  ·  Date: _________

Free printable worksheet with full answer key — or add this module to a class and your students practise it interactively.

This module's key terms are also in a printable crossword (Modules 710–727) and word search (Modules 710–742).

Take this worksheet further — free with Cloodit

Each concept is reinforced from every angle — learned, then applied, then tested.

Learning
Duo
Which line checks if "Ava" is in the array names and gives true or false?
names.includes("Ava")names.indexOf("Ava")
Matches
if (colours.indexOf("red") === 0) { console.log("Found"); }
↓
0 is the position of the first item, not a 'not found' result. Not found is -1, so this misses red at other positions. Use !== -1 to check it was found.
Applying
Meanings
Term
includes(value)
Meaning
Array method that gives true if the value is in the array, otherwise false. Its partner indexOf(value) gives the position, or -1 if the value is not found.
Practice
const team = ["Ava", "Liam", "Mia"];
Testing
Gaps
console.log(pets.("dog"));
Check Answer
Spots
3console.log(i);
4console.log(i === 0);

Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.

The 'Woodpecker' badge — Tap Tap Tap, one of Cloodit's 106 collectible badges
Students earn collectable badges

Over 100 of them, across seven natural biomes.

Study Buddy
Students build a real study resource

Turns their work into revision material ready for Flash Cards and Exam study.

Diamond Dash — one of Cloodit's live, competitive Sprint games
Students lock in with Sprints

Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.

A Cloodit certificate of achievement, ready to personalise and print
Custom printable certificates

Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.

Quick Questions

1. Which line checks if "Ava" is in the array names and gives true or false?
A. names.includes("Ava") ✓
B. names.indexOf("Ava")
includes() gives true or false. indexOf() gives a position number, or -1 if the value is missing.
2. let pets = ["cat", "dog"]; What does pets.includes("fish") give?
A. -1
B. false ✓
includes() only ever gives true or false. -1 is what indexOf() gives when it can't find a value.
3. let nums = [5, 8, 12]; What does nums.indexOf(12) give?
A. 2 ✓
B. true
12 is at index 2 because counting starts at 0. indexOf() gives the position, not true or false.
4. let colours = ["red", "blue"]; What does colours.indexOf("green") give?
A. -1 ✓
B. 0
-1 means not found. 0 would mean the value is the first item in the array.
5. Which if statement correctly checks that "Leo" is NOT in the array team?
A. if (team.indexOf("Leo") === 0)
B. if (team.indexOf("Leo") === -1) ✓
A result of -1 means not found. A result of 0 means the value was found at the very start of the array.

Match the Pairs

fruits.includes("apple") → Gives true if "apple" is in the fruits array, otherwise false
fruits.indexOf("pear") → Gives the position of "pear" in the array, or -1 if it is not found
Use includes inside an if to run code only when a value is in the array → if (names.includes("Zoe")) { console.log("Found"); }
Check that indexOf did not find the value by comparing the result with -1 → if (scores.indexOf(50) === -1) { console.log("Not found"); }
if (colours.indexOf("red") === 0) { console.log("Found"); } → 0 is the position of the first item, not a 'not found' result. Not found is -1, so this misses red at other positions. Use !== -1 to check it was found.
let pos = fruits.includes("kiwi"); → includes gives true or false, not a position. To get the position of kiwi you need indexOf.

Key Term

includes(value)
Array method that gives true if the value is in the array, otherwise false. Its partner indexOf(value) gives the position, or -1 if the value is not found.

Fill in the Blanks

1. const pets = ["cat", "dog", "fish"]; console.log(pets.includes("dog"));
Hint: Type the array method that gives true or false depending on whether a value is in the array.
2. const names = ["Ava", "Liam", "Mia"]; console.log(names.indexOf("Mia"));
Hint: Type the array method that returns the position of a value.
3. const scores = [70, 85, 92]; console.log(scores.indexOf(50) === -1);
Hint: Type the number that indexOf gives when the value is not in the array.
4. const games = ["chess", "tag"]; if (games.includes("tag")) console.log("Found!");
Hint: Type the method that gives a true or false answer, so it can be used as the if condition.
5. const colours = ["red", "blue"]; if (!colours.includes("green")) console.log("Not found");
Hint: Type the operator that reverses a true or false result, so the code runs when the value is missing.

Spot the Error

Question 1
A const n = ["Ava", "Leo"];
B const i = n.indexOf("Zed");
C console.log(i);
D console.log(i === 0);
Line D — indexOf gives -1 when the value is not found, not 0 (0 is the first position). The check should be i === -1.
Question 2
A const n = [4, 8, 15];
B console.log(n.include(8));
C console.log(n.indexOf(8));
Line B — The method is called includes, with an s on the end. include is not an array method and causes a TypeError.
Question 3
A const t = ["a", "b", "c"];
B const pos = t.indexOf("b");
C if (pos === -1) {
D console.log("At", pos);
E }
Line C — The condition is backwards. The code inside should run when the value IS found, so it must be pos !== -1.
Question 4
A const f = ["pie", "tea"];
B const has = f.includes("tea");
C if (has === "true") {
D console.log("Yes");
E }
Line C — includes gives the boolean true, not the string "true", so this comparison is never true. Use if (has) {.
Question 5
A const pets = {"cat", "dog"};
B const i = pets.indexOf("dog");
C console.log(i);
Line A — An array is written with square brackets, not curly braces. It should be const pets = ["cat", "dog"];

Student with a class code? Join your class →