String Search Methods

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

String Search Methods

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Does includes() return true or false?
A Yes
B No
2. Does indexOf() return a boolean like includes() does?
A Yes
B No
3. Does indexOf() return -1 when the substring isn't found?
A Yes
B No
4. Is 'indexof' with a lowercase o valid JavaScript syntax?
A Yes
B No
5. Do these search methods need a substring passed as an argument?
A Yes
B No

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1text.includes("world") AReturns the position where "world" starts in text
2text.indexOf("world") Bword.includes("cat")
3The value returned when a substring isn't found CReturns true if text contains "world"
4Checks whether a substring exists in a string D-1
5text.Includes("world") EWrong - method names are camelCase: indexOf
6text.indexof("world") FWrong - 'includes' must start with a lowercase i

Definitions

includes()

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
console.log(text.________("world"));
2.
console.log(text._______("world"));
3.
let found = word.________("cat");
4.
let pos = word._______("cat");
5.
let hasDog = text.________("dog");

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A let a = "hi there";
B console.log(a.Includes("hi"));
C console.log(a);
Question 2
A let b = "good day";
B console.log(b);
C console.log(b.indexof("day"));
Question 3
A let c = "hi there";
B console.log(c);
C console.log(c.includes);
Question 4
A console.log(d.indexOf);
B let d = "good day";
C console.log(d);
Question 5
A let e = "hi there";
B console.log(e.includes("hi"));
C console.log(e);
D console.log(e.Includes("hi"));

Answer Key

String Search Methods
Section A: Quick Questions
Q1
A — Yes
Q2
B — No
Q3
A — Yes
Q4
B — No
Q5
A — Yes
Section B: Match the Pairs
Q1
text.includes("world") → Returns true if text contains "world"
Q2
text.indexOf("world") → Returns the position where "world" starts in text
Q3
The value returned when a substring isn't found → -1
Q4
Checks whether a substring exists in a string → word.includes("cat")
Q5
text.Includes("world") → Wrong - 'includes' must start with a lowercase i
Q6
text.indexof("world") → Wrong - method names are camelCase: indexOf
Section C: Definitions
Q1
includes() — Checks whether a string contains a given substring, returning true or false.
Section D: Fill in the Blanks
Q1
console.log(text.includes("world"));
Q2
console.log(text.indexOf("world"));
Q3
let found = word.includes("cat");
Q4
let pos = word.indexOf("cat");
Q5
let hasDog = text.includes("dog");
Section E: Spot the Error
Q1
B — 'includes' must be lowercase - a capital I causes an error.
Q2
C — Method names are camelCase - it should be indexOf, not indexof.
Q3
C — Missing parentheses and the search value - includes() needs an argument.
Q4
A — Missing parentheses and the search value - indexOf() needs an argument.
Q5
D — 'includes' must be lowercase - a capital I causes an error.