JavaScript .includes()
Published Mar 21, 2022
Contribute to Docs
The .includes() method returns true if a given value is included in a string. Otherwise, it returns false.
Syntax
string.includes(value, index=0);
The following parameters are used:
- A case-sensitive
valuethat is checked for inclusion in thearray. - An optional
index, defaulted to 0, that tells.includes()where to begin the check.
Here are some edge-cases to consider when using .includes():
- 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.
Example
The .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 false because .includes() is case-sensitive. The string literal world was checked for with a lowercase “w” rather than a capital “W” like in the helloWorld string.
Codebyte Example
In the example below, the .includes() method is applied three times to the myString variable, using a combination of optional parameters:
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn JavaScript on Codecademy
- Front-end engineers work closely with designers to make websites beautiful, functional, and fast.
- Includes 34 Courses
- With Professional Certification
- Beginner Friendly.115 hours
- Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.
- Beginner Friendly.15 hours