Open In App

Underscore.js _.indexOf() Function

Last Updated : 16 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Underscore.js _.indexOf() gives the index of the element whose position we need to find. It starts to count the position of the elements in the array from 0. If the element is not present in the array then the result will be -1.

Syntax: 

_.indexOf(array, value, [isSorted]);

Parameters:

  • array: It is the array, in which we want to find out the index of the particular item.
  • value: It is the value of the particular item whose index we want to get.
  • isSorted(optional): It is the flag used when the passed array is sorted.

Return value:

It returns the position of the passed element and -1 if the element is not found in the array. 

Passing a list of numbers to _.indexOf() function:

The _.indexOf() function takes the element from the list one by one and checks whether it is equal to the passed element in the second parameter. If it is equal then the result is it’s index otherwise -1 is returned.

Example: The below code implements the _.indexOf() function with a numbers array.

html




<!DOCTYPE html>
<html>
 
<head>
    <script src=
    </script>
</head>
 
<body>
    <script type="text/javascript">
        console.log(_.indexOf([1, 2, 3, 4, 5, 6], 4));
    </script>
</body>
 
</html>


Output:

Passing a list of strings to the _.indexOf() function:

We can also pass the list of characters to the _.indexOf() function and it will work in the same way as the numbers list works. In the second parameter we need to mention the word whose index we need to find inside the quotes.

Example: The below code implements the _.indexOf() function with a strings array.

html




<!DOCTYPE html>
<html>
 
<head>
    <script src=
    </script>
</head>
 
<body>
    <script type="text/javascript">
        console.log(_.indexOf(['HTML', 'CSS',
            'JS', 'AJAX', 'PEARL'], 'AJAX'));
    </script>
</body>
 
</html>


Output:

Passing second parameter which is not present in the list:

Pass the list of character elements to the _.indexOf() function. Since in the given list the second parameter ‘GEEKS’ is not present so the result will be -1.

Example: The below code example explains the behaviour of the _.indexOf() function with a parameter which is not present in the list. 

html




<!DOCTYPE html>
<html>
 
<head>
    <script src=
    </script>
</head>
 
<body>
    <script type="text/javascript">
        console.log(_.indexOf(['HTML', 'CSS', 'JS',
            'AJAX', 'PEARL'], 'GEEKS'));
    </script>
</body>
 
</html>


Output:

Passing a list with repeated elements to the _.indexOf() function:

Even though we pass an array with the repeated element the _.indexOf() function will work in the same way and return the index of the first appearance of the passed element.

Example: The below code implements the _.indexOf() function by passing an array that contains repeated elements.

html




<!DOCTYPE html>
<html>
 
<head>
    <script src=
    </script>
</head>
 
<body>
    <script type="text/javascript">
        console.log(_.indexOf(['HTML', 'CSS', 'JS', 'AJAX',
            'PEARL', 'CSS', 'HTML', 'CSS'], 'CSS'));
    </script>
</body>
 
</html>


Output:



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads