.includes() method returns
true if a given value is included in a string. Otherwise, it returns
The following parameters are used:
- A case-sensitive
valuethat is checked for inclusion in the
- An optional
index, defaulted to 0, that tells
.includes()where to begin the check.
Here are some edge-cases to consider when using
- It will not work if the provided
indexis greater than the length of the string. Instead,
falsewill be returned.
- If the
indexis less than or equal to 0, the entire string will be searched.
.includes() method can be used in a few ways. First, it can be applied directly to a string:
console.log('Hello World!'.includes('World'));// Output: true
It can also be used with a string value assigned to a variable:
const helloWorld = 'Hello World!';console.log(helloWorld.includes('world'));// Output: false
The output above is
.includes() is case-sensitive. The string literal
world was checked for with a lowercase “w” rather than a capital “W” like in the
In the example below, the
.includes() method is applied three times to the
myString variable, using a combination of optional parameters: