JavaScript String indexOf()
Syntax & Examples


String.indexOf() method

The indexOf() method of the String class in JavaScript returns the index within the calling String object of the first occurrence of the specified value, searchString, starting the search at fromIndex. Returns -1 if the value is not found.


Syntax of String.indexOf()

There are 2 variations for the syntax of String.indexOf() method. They are:

1.
indexOf(searchString)

Parameters

ParameterOptional/RequiredDescription
searchStringrequiredThe string to search for within this string.

This method returns the index within the calling String object of the first occurrence of searchString.

Returns value of type Number.

2.
indexOf(searchString, fromIndex)

Parameters

ParameterOptional/RequiredDescription
searchStringrequiredThe string to search for within this string.
fromIndexoptionalThe position in the calling string to start the search from. Defaults to 0.

This method returns the index within the calling String object of the first occurrence of searchString, starting the search at fromIndex.

Returns value of type Number.



✐ Examples

1 Using indexOf() method to find the first occurrence of a substring

In JavaScript, we can use the indexOf() method to find the first occurrence of a specified substring.

For example,

  1. We define a string variable str with the value 'Hello, world!'.
  2. We use the indexOf() method with the argument 'world' to find the first occurrence of the substring.
  3. The result is stored in the variable index.
  4. We log index to the console using the console.log() method.

JavaScript Program

const str = 'Hello, world!';
const index = str.indexOf('world');
console.log(index);

Output

7

2 Using indexOf() method with a specified start position

In JavaScript, we can use the indexOf() method to find the first occurrence of a specified substring, starting from a given position.

For example,

  1. We define a string variable str with the value 'Hello, world!'.
  2. We use the indexOf() method with the arguments 'o' and 5 to find the first occurrence of the character 'o' starting from position 5.
  3. The result is stored in the variable index.
  4. We log index to the console using the console.log() method.

JavaScript Program

const str = 'Hello, world!';
const index = str.indexOf('o', 5);
console.log(index);

Output

8

3 Using indexOf() method to search for a non-existent substring

In JavaScript, we can use the indexOf() method to search for a substring that does not exist in the string.

For example,

  1. We define a string variable str with the value 'Hello, world!'.
  2. We use the indexOf() method with the argument 'JavaScript' to search for the substring 'JavaScript' within the string.
  3. The result is stored in the variable index.
  4. We log index to the console using the console.log() method.

JavaScript Program

const str = 'Hello, world!';
const index = str.indexOf('JavaScript');
console.log(index);

Output

-1

Summary

In this JavaScript tutorial, we learned about indexOf() method of String: the syntax and few working examples with output and detailed explanation for each example.