JavaScript indexOf method simplified

In this short tutorial, we will be looking at what the indexOf() method in JavaScript is about. We will be looking into its syntax, different methods with examples.

Table of Contents - JavaScript indexOf

  • What is the indexOf method in Javascript?
  • Syntax and parameters
  • Using the JavaScript indexOf method on strings
  • Using indexOf() to count occurrences of a substring in a string
  • The indexOf() method and it’s case-sensitivity
  • Closing thoughts

What is the indexOf method in Javascript?

The indexOf() method is used to return the first occurrence of a specified value in a string. In case the specified value is not found, it returns -1. Do keep in mind that the JavaScript indexOf() method is case sensitive.

Syntax and parameters

Here is the syntax of the indexOf() method:

Syntax:

string.indexOf(searchvalue, start)

Now let’s look at what the parameters of this method signify.

- searchvalue: The value/element entered here will be searched for. This is a required parameter

- start: Here, one can define the position from which the search is to start from. This is an optional parameter, to be used as per the need.

Return value: The value that we receive when the method is executed is the first position where the searched value (searchvalue) is found. However, we would receive a -1 if it doesn’t occur in the searched string.

Using the JavaScript indexOf method on strings

Let’s consider an example where we use this method to get the index of the first occurrence of the substring “flexiple.”

Input:

let devs = 'Join as a freelance developer at Flexiple';
let index = devs.indexOf('Flexiple');
        
console.log(index);

Output:

33

So what is an index position? An Index number indicates the value contained at an Index position. In this case, the element 'J' of the word 'Join' would have the index value 0 and the character ‘o’ would have the index value 1 and so on.

Using indexOf() to count occurrences of a substring in a string

In another example given below, you would be able to see how one could count the number of instances or occurrences of a specified substring that appears in a string.

Input:

let devs = 'Flexiple is hiring! Join Flexiple as a freelance developer to work on client projects at Flexiple';
let substri = 'Flexiple';
        
let count = 0;
        
let index = devs.indexOf(substri);
while(index !== -1) {
    count++;
    index = devs.indexOf(substri, index + 1);
}
        
console.log(count);

Output:

3

Firstly, the indexOf() method finds the first occurrence of substri in devs. Then, the while loop is run repeatedly to find the next position of substri in devs starting from the last found position +1. Finally, the output is given as 3 as the number of times “Flexiple” was found in the string devs.

The indexOf() method and it’s case-sensitivity

In this section, let’s look at another example to understand the case-sensitivity of the method.

Input:

let devs = 'FLEXIPLE indexof';
let substri = 'flexiple';
        
let index = devs.indexOf(substri);
        
console.log(index);

Output:

-1

In this example, we see that the method returns -1 as the output. This is because “FLEXIPLE indexof” does not contain the substring “flexiple”, but “FLEXIPLE”

So, if we want to perform a case-sensitive search with the indexOf() method, we can convert both the substring and string to lowercase before using the method. Let’s look at another example to understand this better.

Input:

let devs = 'FLEXIPLE indexOf';
let substri = 'flexiple';
        
let index = devs.toLocaleLowerCase().indexOf(substri.toLocaleLowerCase());
        
console.log(index);

Output:

0

Closing thoughts

In this tutorial, we have seen how the indexOf() works, it’s syntax and parameters. We have also understood how the method is used to count occurrences of substrings. Finally we check how the function’s case-sensitivity can be circumvented as per our need.

The same method can be applied to arrays apart from strings like we’ve seen in this tutorial.