Open In App

JavaScript String includes() Method

Last Updated : 10 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

The includes() method returns true if a string contains a specified string. 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: The code checks if the string “Geeks” is present in the string “Welcome to GeeksforGeeks.”. It then logs the result, which is true, since “Geeks” is indeed present in the string.

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

Output
true

Example 2: Here, 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: The code checks if the character “o” is present in the string “Welcome to GeeksforGeeks.” starting from index 17. It then logs the result, which is false, since “o” is not present in the substring starting from index 17.

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

Output
true

Example 4: 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:


Next Article

Similar Reads