This JavaScript tutorial explains how to use the string approach known as includes() with syntax and examples.
Description
In JavaScript, includes() is a string method that determines whether a substring is discovered in a string. Because the includes() technique is a technique of the String object, it should be invoked via a unique instance of the String class.
Syntax
In JavaScript, the syntax for the includes() technique is:
string.includes(substring [, start_position]);
Parameters or Arguments
substring It is the substring that you desire to search for inside string. start_position Optional. It is the position in string where the search will start. The first position in string is zero If this parameter is no longer provided, the search will start at the opening of string.
Returns
The includes() method returns authentic if the substring is discovered in string. Otherwise, it returns false.
Note
The includes() method performs a case-sensitive search. The includes() approach does not alternate the fee of the original string.
Example
Let’s take a look at an instance of how to use the includes() method in JavaScript.
For example:
var totn_string = 'TechOnTheNet';
console.log(totn_string.includes('e'));
In this example, we have declared a variable called totn_string that is assigned the string cost of ‘TechOnTheNet’. We have then invoked the includes() approach of the totn_string variable to determine if a substring is located within totn_string.
We have written the output of the includes() method to the web browser console log, for demonstration purposes, to exhibit what the includes() technique returns.
The following will be output to the net browser console log:
true
In this example, the includes() technique returned real because the substring ‘e’ is located within ‘TechOnTheNet’ when performing a case-sensitive search.
Specifying a Start Position Parameter
You can change the position where the search will start in the string via providing a start_position parameter to the includes() method.
For example:
var totn_string = 'TechOnTheNet';
console.log(totn_string.includes('e',11));
The following will be output to the web browser console log:
false
In this example, we have set the start_position parameter to a cost of eleven This ability that the search will start searching for the fee ‘e’ starting at position eleven in the string ‘TechOnTheNet’. So in this case, the includes() approach lower back false because the substring ‘e’ is no longer found.
Specifying Multiple Characters as the Substring
Finally, the includes() technique can search for more than one characters in a string.
For example:
var totn_string = 'TechOnTheNet';
console.log(totn_string.includes('The'));
The following will be output to the web browser console log:
true
In this example, the includes() technique returned authentic because the substring ‘The’ is located in the string ‘TechOnTheNet’.
Leave a Review