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.icludes(SEARCH_TERM,START);
  • `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.

var greeting="Hello my name is poftut.com. You can find very good tutorials about IT on me.";

var match = greeting.includes("poftut.com");

console.log(match);
//Prints true



var match = "Hello my name is poftut.com. You can find very good tutorials about IT on me.".includes("poftut.com");

console.log(match);
//Prints true



var greeting="Hello my name is poftut.com. You can find very good tutorials about IT on me.";

var match = greeting.includes("POFTUT.COM");

console.log(match);
//Prints false
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.

LEARN MORE  Python String find() Function Tutorial with Examples

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.

var greeting="Hello my name is poftut.com. You can find very good tutorials about IT on me.";

var match = greeting.includes("poftut.com",10);

console.log(match);
//Print to the console true

var match = greeting.includes("poftut.com",30);

console.log(match);
//Print to the console false
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.

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.

"Hello my name is poftut.com. You can find very good tutorials about IT on me.".includes("poftut.com");
//Evaluated as true
1 == "Hello my name is poftut.com. You can find very good tutorials about IT on me.".includes("poftut.com");
//Evaluated as true
1 == "Hello my name is poftut.com. You can find very good tutorials about IT on me.".includes("POFTUT.COM");
//Evaluated as false
-11 == "Hello my name is poftut.com. You can find very good tutorials about IT on me.".includes("POFTUT.COM");
//Evaluated as false
-1 == "Hello my name is poftut.com. You can find very good tutorials about IT on me.".includes("POFTUT.COM");
//Evaluated as false
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.

var greeting="Hello my name is poftut.com. You can find very good tutorials about IT on me.";

index=greeting.indexOf("poftut.com");

console.log(index);
//Output 17

index=greeting.indexOf("POFTUT.COM");

console.log(index);
//Output -1
includes() Alternative indexOf()  Function
includes() Alternative indexOf()  Function

Leave a Comment