indexOf() in JavaScript

Learn via video course
FREE
View all courses
JavaScript Course With Certification: Unlocking the Power of JavaScript
JavaScript Course With Certification: Unlocking the Power of JavaScript
by Mrinal Bhattacharya
1000
4.8
Start Learning
JavaScript Course With Certification: Unlocking the Power of JavaScript
JavaScript Course With Certification: Unlocking the Power of JavaScript
by Mrinal Bhattacharya
1000
4.8
Start Learning
Topics Covered

Overview

The indexOf method in JavaScript is mainly used in string searching operations. The indexOf() method of the String class returns the index of the first occurrence of the substring in a string. If the substring is not found in the string, it returns -1.

Syntax of String indexOf() in Javascript

The syntax of indexOf in JavaScript is as follows:

Here str is the string on which indexOf() is called.

Parameters of String indexOf() in Javascript

indexOf in JavaScript method takes two parameters:

  • searchValue(): the substring to be searched inside the string. In case no value is provided, undefined is searched.
  • index(Optional): This is the starting index, from where the search for the searchValue substring begins in the string. The default value is 0.

Return Value of String indexOf() in Javascript

Return Type: number

IndexOf in JavaScript returns the index at which the first occurrence of the substring passed as the parameter, i.e. searchValue is found, and if the substring is not found in the string, it returns -1.

Example

Output:

So as you can see, in the above example, the first occurrence of "W" in the string i.e., 6 is returned.

What is String indexOf() in Javascript?

JavaScript provides numerous predefined built-in methods to perform various string-related operations.indexOf() method is useful in string search operations. As discussed above, given a string and a substring, the indexOf() method searches for the occurrence of the substring in the string and returns its first occurrence in the string. The value returned by the method is 0-based. In case the substring is absent in the string,-1 is returned. Let us explore more about the method! String indexOf() in Javascript

More Examples

Example 1 : Using indexOf()

Let us consider a situation where you have a string with some missing punctuation. You need to add a comma (,) character right before "while" inside the string. This can be done by finding the position of "while" using indexOf(). Let us look at the implementation:

Output:

In the above example, we called the method indexOf() of on string message, and the first occurrence of the passed parameter is returned.

Example 2 : Using indexOf() if passed-on parameter is not Present

In case the passed-on parameter is not present, let us see what the method returns.

Output:

So as you can see, since the substring is absent, -1 is returned.

Example 3 : Using indexOf() with Specified Search Value Index

In the above cases, we just specified the substring to be searched. Another parameter can be added to specify the index from where the search should start.

Output:

In the above case, the substring to be searched, i.e., 'JavaScript' is present twice in the string, but since the passed value of the parameter starting index is '6' thus, the first occurrence of the substring present before this index is not considered.

Example 4: Using indexOf() to Count Occurrences of a Letter in a String

There can be multiple occurrences of a letter inside the string. Let us use the method indexOf() to find all the occurrences of a particular letter. We shall declare a function that takes string and substring to be searched as arguments. The first occurrence of substring is calculated at first and then iteration takes place over the whole length of the string. The calculated positions are pushed into an array. During the iteration the value of starting index keeps incrementing.

Output:

So in the above example, all the occurrences of substring "JavaScript" are found using the indexOf() method.

Browser compatibility

The indexOf() method in javascript is supported by all the browsers such as Chrome, Edge, Safari, Opera, etc.

Conclusion

  • Given a string and a substring, the indexOf() method searches for the occurrence of the substring in the string and returns its first occurrence in the string
  • Its major applications are the searching operations.
  • It takes two parameters, the substring to be searched and an optional one, the starting index from which the search should begin.
  • In case the substring is not found, it returns -1.
  • Value returned is 0-based (as it considers 0-based indexing).