Open In App
Related Articles

JavaScript String includes() Method

Improve Article
Improve
Save Article
Save
Like Article
Like

JavaScript String includes() method determines whether a string contains whether the given character is present in the given string or not. This method returns true if the string contains the characters, otherwise, it returns false. 

Note: The includes() method is case sensitive i.e. it will treat the Uppercase Characters and Lowercase Characters differently.

Syntax:  

string.includes(searchvalue, start)

Parameters: 

  • search value: It is the string in which the search will take place.
  • start: This is the position from where the search will be processed 
    (although this parameter is not necessary if this is not mentioned the search will begin from the start of the string).

Return Value:

Returns either a Boolean true indicating the presence or it returns a false indicating the absence.

Example 1: This method shows the basic use of the String includes() Method. 

javascript




let str = "Welcome to GeeksforGeeks.";
let check = str.includes("Geeks");
console.log(check);


Output

true

Example 2: In this case, the second parameter is not defined, so the search will take place from the starting index. But as this method is case sensitive it will treat the two strings differently, hence returning a boolean false. 

javascript




let str = "Welcome to GeeksforGeeks.";
let check = str.includes("geeks");
console.log(check);


Output

false

Example 3: This example checks for a string at a fixed position in the str variable.

javascript




let str = "Welcome to GeeksforGeeks.";
let check = str.includes("o", 17);
console.log(check);


Output

true

Example 4: This example checks for a string in the str variable and returns false as it is not found at the specified position.

javascript




let str = "Welcome to GeeksforGeeks.";
let check = str.includes("o", 18);
console.log(check);


Output

false

Exceptions:

The search will not be processed if the second parameter i.e. computed index (starting index) is greater than or equal to the string length and hence return false. 

Example 1: This example describes the above-explained approach.

javascript




let str = "Welcome to GeeksforGeeks.";
let check = str.includes("o", 25);
console.log(check);


Output

false

Example 2: If the computed index (starting index) i.e. the position from which the search will begin is less than 0, the entire array will be searched. 

javascript




let str = "Welcome to GeeksforGeeks.";
let check = str.includes("o", -2);
console.log(check);


Output

true

Supported Browsers:

  • Chrome 41 and above
  • Edge 12 and above
  • Firefox 40 and above
  • Opera 28 and above
  • Safari 9 and above

We have a complete list of Javascript string methods, to check those please go through this Javascript String Complete reference article.

JavaScript is best known for web page development but it is also used in a variety of non-browser environments. You can learn JavaScript from the ground up by following this JavaScript Tutorial and JavaScript Examples.


Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 04 Dec, 2023
Like Article
Save Article
Previous
Next
Similar Reads
Complete Tutorials