How to Use indexOf Method in JavaScript

indexOf() method is used to search for the index of an element in a given array; it can also be used to find the position of a character or a substring in a string. The indexOf() method returns the position of the element/substring if it is found; else, it returns -1. The indexing starts from 0, so the first element of an array and the first word in a string always return 0.

How to use the indexOf() method

In this guide, we will learn how to use the indexOf() method to find the index of an element/substring in an array/string; but first, let’s discuss its syntax:

array_name.indexOf(element, starting_point)
string_name.indexOf(searchvalue, starting_point)

The indexOf() method takes two parameters:

  • element/searchvalue: The first parameter is required. It can be an element of an array or a substring of which the index is required.
  • starting_point: This parameter is optional. It tells the method to start the search from the specified point. It is 0 by default.

Now will use the indexOf() method to find the index of an element in an array as an example:

Note: We will use the browser console to demonstrate examples performed in this post. To open up the browser console:

  • Use the F12 key in Chrome and other chromium-based browsers.
  • Use CTRL + SHIFT + K keyboard shortcut keys for Mozilla.
  • Use Option + ⌘ + C keyboard shortcut keys in Safari (if developer menu does not appear, then open Preferences by pressing ⌘ +, and in Advanced tab check “Show Develop menu in menu bar”).


var animals = ['Lion', 'Monkey', 'Rhino', 'Cat'];

Similarly, we can also use this method on a string:

var str = 'Welcome to Linux Hint!';

It can also be used to find a single character:

var str = 'Welcome to Linux Hint!';

The indexOf() method by default starts searching from 0; but we can pass the starting point as an argument as well:

var animals = ['Lion', 'Monkey', 'Rhino', 'Cat'];
console.log(animals.indexOf('Rhino', 3));

The method has returned -1 as it cannot find ‘Rhino’ if it starts the search from 3. Now, if we change the starting point to 2, then:

var animals = ['Lion', 'Monkey', 'Rhino', 'Cat'];
console.log(animals.indexOf('Rhino', 2));

Similarly for strings:

var str = 'Welcome to Linux Hint!';
console.log(str.indexOf('c', 5));

var str = 'Welcome to Linux Hint!';
console.log(str.indexOf('c', 1));

The index() method starts searching from 0 to the end of an array/string; and returns the index of the first occurrence of the search value. If there are two similar items in an array or a string and you want to find the index of the last one, then you should use the lastIndexOf() method:

var str = 'Welcome to Linux Hint!';

Similarly for an array:

var animals = ['Lion', 'Monkey', 'Rhino', 'Cat', 'Lion'];

The indexOf() method is case-sensitive.


While programming, when working with arrays or strings, we often need to find the index of a specific element or a substring. The indexOf() method comes in handy in such situations.

In this how-to guide, we have learned how to use the indexOf() method to find the index of an item in a string/array. Moreover, we also discussed the type and parameters we can pass to the indexOf() method.

About the author

Shehroz Azam

A Javascript Developer & Linux enthusiast with 4 years of industrial experience and proven know-how to combine creative and usability viewpoints resulting in world-class web applications. I have experience working with Vue, React & Node.js & currently working on article writing and video creation.