This JavaScript tutorial explains how to use the string technique called indexOf() with syntax and examples.
Description
In JavaScript, indexOf() is a string method that is used to locate the location of a substring in a string. Because the indexOf() approach is a technique of the String object, it need to be invoked thru a precise occasion of the String class.
Syntax
In JavaScript, the syntax for the indexOf() technique is:
string.indexOf(substring [, start_position]);
Parameters or Arguments
substring It is the substring that you choose to discover within string. start_position Optional. It is the function in string the place the search will start. The first function in string is zero If this parameter is no longer provided, the search will start at the beginning of string and the full string will be searched.
Returns
The indexOf() method returns the position of the first occurrence of substring in string. The first function in the string is 0
If the indexOf() method does now not find the substring in string, it will return -1.
Note
The indexOf() method performs a case-sensitive search. The indexOf() technique does not exchange the fee of the original string.
Example
Let’s take a look at an instance of how to use the indexOf() method in JavaScript.
For example:
var totn_string = 'TechOnTheNet';
console.log(totn_string.indexOf('t'));
In this example, we have declared a variable called totn_string that is assigned the string value of ‘TechOnTheNet’. We have then invoked the indexOf() approach of the totn_string variable to seem to be for a substring inside totn_string.
We have written the output of the indexOf() technique to the web browser console log, for demonstration purposes, to show what the indexOf() method returns.
The following will be output to the net browser console log:
11
In this example, the indexOf() technique back eleven because the first prevalence of ‘t’ inside ‘TechOnTheNet’ is position eleven in the string.
Specifying a Start Position Parameter
You can trade the function where the search will start in the string by offering a start_position parameter to the indexOf() method.
For example:
var totn_string = 'TechOnTheNet';
console.log(totn_string.indexOf('T',4));
The following will be output to the net browser console log:
6
In this example, we have set the start_position parameter to a value of 4 This skill that the search will begin looking for the cost ‘T’ beginning at role 4 in the string. So in this case, the substring ‘T’ is discovered at role 6 in the string ‘TechOnTheNet’.
Specifying Multiple Characters as the Substring
Next, the indexOf() technique can search for more than one characters in a string.
For example:
var totn_string = 'TechOnTheNet';
console.log(totn_string.indexOf('The'));
The following will be output to the internet browser console log:
6
In this example, the indexOf() method returned 6 which is the function of ‘The’ in the string ‘TechOnTheNet’.
Since the indexOf() method can solely return one value, it will return the position of the substring’s first character when the incidence is found, even even though the substring is a couple of characters in length.
No Occurrence is Found
Finally, the indexOf() approach will return -1 if an occurrence of substring is now not determined in string.
For example:
var totn_string = 'TechOnTheNet';
console.log(totn_string.indexOf('z'));
The following will be output to the internet browser console log:
-1
In this example, the indexOf() approach lower back -1 because the substring ‘z’ is now not discovered in the string ‘TechOnTheNet’.
Leave a Review