JavaScript String includes() Method

Learn via video course
FREE
View all courses
JavaScript Course With Certification: Unlocking the Power of JavaScript
JavaScript Course With Certification: Unlocking the Power of JavaScript
by Mrinal Bhattacharya
1000
4.8
Start Learning
JavaScript Course With Certification: Unlocking the Power of JavaScript
JavaScript Course With Certification: Unlocking the Power of JavaScript
by Mrinal Bhattacharya
1000
4.8
Start Learning
Topics Covered

Overview

The includes() function is used to check if the set of characters (string) is present inside another string or not. It returns true if it founds the set of characters in the string, else returns false. The includes() function in javascript is a case-sensitive operation, thus it treats uppercase alphabets differently from lower case alphabets.

Syntax of String includes() in Javascript

To use the string includes in javascript, we need to use the name of the string on which the search will be done, after that we use the includes(), method and in the parameters, we will use the string that is to be searched and the starting index(optional)

Parameters of String Includes() in Javascript

For the parameter of string includes() function in javascript, we use the "set of characters"(string) that we want to search and the second parameter is optional that is, the starting index (the index from where the search will begin in the original string). By default, the value of the starting index is zero.

Return Value of String includes() in Javascript

The string includes() method in javascript returns boolean values only. It returns true if the set of characters(string) that is being searched is found in the original string, else it returns false.

Exceptions of String includes() in Javascript

There are no exceptions in the string includes() function in javascript.

Example

Here in this example, we will make a string "s1" and then by using the includes() method, we will find if the string "SCALER" is present in the string s1 or not. The expected output is "true".

Note: In this example, we haven't used the second parameter (starting_index). So, the default value "0" will be used

Output:

In this example, we will use the same string "s1" and will find the same "SCALER" string. But, in this case, we will use the starting_index parameter, and will set it to "7". The expected output is "false" as the search string starts from index "4".

Output:

What is String includes() Method in Javascript?

The string includes() in javascript is a function that is used to check if the set of characters (string) is present inside another string or not.

Case-Sensitivity

The string includes() method in javascript is a case-sensitive method. It looks for the exact characters with an exact case of the search_string in the original string. The second line of the code will return true in the console, but as the includes() method is case-sensitive the third line of code will return false.

Output:

Polyfill

A polyfill is a set of codes in javascript which is used to provide modern functionality on older browsers that do not natively support it. The includes() method can be polyfill:

Special Cases of includes() Function

There are two special cases of string included in javascript, Both are based on the second parameter that is passed to the function, which is starting_index.

If the starting_index is greater than or equal to the length of the siring, the function will return false as the search will not be processed.

If the starting_index given is less than 0, then the entire array will be traversed.

In this example, we will understand the first special case, i.e. we will take the starting_index more than the length of the string. In this case, the output will be "false".

Output:

In this example, we will understand the second special case, i.e. we will take the starting_index less than "0". In this case, the output will be "true" if the search_string is present.

Output:

Conclusion

  • The string includes() function in javascript is used to check if the set of characters (string) is present inside another string.
  • The string includes in javascript returns only boolean values. True if the string is found and false if the string is not found.
  • The string includes in javascript has two parameters:
    • Search_string : The string which is to be searched in the original string.
    • starting_index: The index from which the searching will start.
  • If the value of starting_index is more than the length of the string, the string included in javascript will return false.
  • If the value of starting_index is less than zero, the includes() function will traverse the entire string.
  • The string includes() method in javascript is a case-sensitive method.