curriculum/challenges/english/blocks/lecture-working-with-string-search-and-slice-methods/67326c0d7bef01c539120766.md
When working with strings in JavaScript, there are many cases where you might need to check whether a string contains a specific substring, which is a smaller part of that string.
For example, you might want to check if a user's input includes a specific word or character before performing some action. One way to achieve this is by using the includes() method.
The includes() method is used to check if a string contains a specific substring. If the substring is found within the string, the method returns true otherwise, it returns false.
Here's the basic syntax:
string.includes(searchValue);
For the syntax, the searchValue is the substring you want to look for within the string. And here's an example:
:::interactive_editor
let phrase = "JavaScript is awesome!";
let result = phrase.includes("awesome");
console.log(result); // true
:::
In this example, the word awesome is found within the string JavaScript is awesome!, so the includes() method returns true.
It's important to note that the includes() method is case-sensitive. This means that the exact match of the characters is required, including their case.
For example:
:::interactive_editor
let phrase = "JavaScript is awesome!";
let result = phrase.includes("Awesome");
console.log(result); // false
:::
Since Awesome (with an uppercase A) does not match awesome (with a lowercase a), the result is false.
You can also use the includes() method to check for a substring starting at a specific index in the string by providing a second parameter:
:::interactive_editor
let text = "Hello, JavaScript world!";
let result = text.includes("JavaScript", 7);
console.log(result); // true
:::
Here, the search for the substring JavaScript starts from the 7th position in the string, ensuring it skips any characters before this position.
The includes() method only returns a true or false result. It does not provide information on where the substring is located in the string or how many times it occurs. If you need that level of detail, other methods, such as the indexOf() method might be more suitable.
What does the includes() method return when a substring is found in a string?
The index of the substring.
Consider what the method does when it detects the substring.
The length of the substring.
Consider what the method does when it detects the substring.
true
false
Consider what the method does when it detects the substring.
3
Which of the following statements about the includes() method is correct?
It is case-insensitive.
Think about whether it distinguishes between uppercase and lowercase characters.
It is case-sensitive.
It replaces the found substring with another value.
Think about whether it distinguishes between uppercase and lowercase characters.
It returns the number of occurrences of the substring.
Think about whether it distinguishes between uppercase and lowercase characters.
2
What will the following code output?
let message = "JavaScript is great!";
let result = message.includes("script");
console.log(result);
true
Focus on whether includes() is case-sensitive.
false
undefined
Focus on whether includes() is case-sensitive.
Throws an error.
Focus on whether includes() is case-sensitive.
2