JavaScript String includes() Function Tutorial with Examples – POFTUT

JavaScript String includes() Function Tutorial with Examples

JavaScript provides the includes() function in order to search a given string for a specific substring. There are also other methods in order to search a string array for specific string too.

includes() Function Syntax

includes() function is provided by a string variable or strings literal in order to find given search term in the given string.

  • STRING is a string variable or string literal where the SEARCH_TERM will be searched.
  • includes() is the function we will use with the following parameters. This function will return boolean results true and false according to the situation and match. If it is not matched it will return false where there is a match it will return true.
  • SEARCH_TERM is the term we will search in the STRING which can be a string variable or string literal.
  • START is the start index number of the search wherefrom the specified START index the search will start in the STRING. The START is optional where if it is not provided the search will start from the beginning of the STRING.

Search Given Term In the Whole String

We will start with a simple example where we will search for a simple term in the given string. In this example, we will create a string variable greeting and search the “poftut.com” inside the greeting variable with the includes() function.

Search Given Term In the Whole String

Search Given Term In the Whole String

includes() is a case insensitive function where “poftut.com” and “POFTUT.COM” are not the same. So they will not match in a search.

Search Given Term In the Specified Part Of The String

includes() function also accepts the search start index where the search will be performed after that index. In the following example, we will search the term “poftut.com” after 10th character.

Search Given Term In the Specified Part Of The String

Search Given Term In the Specified Part Of The String

We can see from the examples that when the index is specified as 10 the given string will match and includes() function will return true. If we specify the index as 30 it will not match and return false.

LEARN MORE  What Is String Data Type In JavaScript, Java, Python, C# , PHP, C, C++, PowerShell Programming Languages?

Comparing includes() Function Result

As includes() function return boolean values like true and false we can compare these results with numbers like 1 and -1 which are related to boolean logic in JavaScript. -1 represents false on the other hand 1 represents true.

Comparing includes() Function Result

Comparing includes() Function Result

includes() Alternative indexOf()  Function

indexOf() function is an alternative to the includes() function where the starting index number of the given term is returned. If there is not match -1 will be returned.

includes() Alternative indexOf()  Function

includes() Alternative indexOf()  Function

Leave a Reply

Your email address will not be published. Required fields are marked *

Enjoy this blog? Please spread the word :)