totn JavaScript

JavaScript: String indexOf() method

This JavaScript tutorial explains how to use the string method called indexOf() with syntax and examples.

Description

In JavaScript, indexOf() is a string method that is used to find the location of a substring in a string. Because the indexOf() method is a method of the String object, it must be invoked through a particular instance of the String class.

Syntax

In JavaScript, the syntax for the indexOf() method is:

string.indexOf(substring [, start_position]);

Parameters or Arguments

substring
It is the substring that you want to find within string.
start_position
Optional. It is the position in string where the search will start. The first position in string is 0. If this parameter is not 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 position in the string is 0.

If the indexOf() method does not find the substring in string, it will return -1.

Note

  • The indexOf() method performs a case-sensitive search.
  • The indexOf() method does not change the value of the original string.

Example

Let's take a look at an example 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() method of the totn_string variable to look for a substring within totn_string.

We have written the output of the indexOf() method to the web browser console log, for demonstration purposes, to show what the indexOf() method returns.

The following will be output to the web browser console log:

11

In this example, the indexOf() method returned 11 because the first occurrence of 't' within 'TechOnTheNet' is position 11 in the string.

Specifying a Start Position Parameter

You can change the position where the search will start in the string by providing 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 web browser console log:

6

In this example, we have set the start_position parameter to a value of 4. This means that the search will begin looking for the value 'T' starting at position 4 in the string. So in this case, the substring 'T' is found at position 6 in the string 'TechOnTheNet'.

Specifying Multiple Characters as the Substring

Next, the indexOf() method can search for multiple characters in a string.

For example:

var totn_string = 'TechOnTheNet';

console.log(totn_string.indexOf('The'));

The following will be output to the web browser console log:

6

In this example, the indexOf() method returned 6 which is the position of 'The' in the string 'TechOnTheNet'.

Since the indexOf() method can only return one value, it will return the position of the substring's first character when the occurrence is found, even though the substring is multiple characters in length.

No Occurrence is Found

Finally, the indexOf() method will return -1 if an occurrence of substring is not found in string.

For example:

var totn_string = 'TechOnTheNet';

console.log(totn_string.indexOf('z'));

The following will be output to the web browser console log:

-1

In this example, the indexOf() method returned -1 because the substring 'z' is not found in the string 'TechOnTheNet'.