Skip to main content

indexOf() JavaScript Array Method – How to Get Text Index

Whenever you use indexOf() on an array, the method does the following:

  1. It searches its calling array for the method's first argument.
  2. It returns the index position of the first match, or -1 if the method found no match.
note

A calling array is an array on which you used indexOf(). So, in bestColorsList.indexOf("white"), bestColorsList is the calling array.

Syntax of the indexOf() Method

Here is indexOf()'s syntax:

callingArray.indexOf(valueToFind, startIndex)

The snippet above shows that indexOf() accepts two arguments: a valueToFind and a startIndex.

Argument 1: valueToFind

A valueToFind is the first argument accepted by the indexOf() method. It defines the value you wish to find in the calling array.

Example 1: Find the index of Tuesday

["Sunday", "Tuesday", "Friday"].indexOf("Tuesday");

// The invocation above will return:
1

Try it on StackBlitz

Example 2: Find the index of Sunday

["Sunday", "Tuesday", "Friday"].indexOf("Sunday");

// The invocation above will return:
0

Try it on StackBlitz

Example 3: Find the index of 5

[1, 3, 5, 7].indexOf(5);

// The invocation above will return:
2

Try it on StackBlitz

Argument 2: startIndex

The startIndex argument is optional. It specifies the index position where you want the computer to start searching for the valueToFind argument.

note
  • The startIndex argument's default value is 0. Therefore, if omitted, the search will begin at index 0.
  • The indexOf() method always starts its search from left to right irrespective of whether startIndex is a positive or negative number.

Example 1: Find the index of Tuesday from the 3rd index position

const daysOfWeek = ["Sunday", "Tuesday", "Thursday", "Friday", "Tuesday", "Sunday", "Tuesday", "Friday"];

daysOfWeek.indexOf("Tuesday", 3);

// The invocation above will return:
4

Try it on StackBlitz

Suppose you specify a negative startIndex argument. In that case, the computer will start the index count from the calling array's last element.

Here's an example:

const daysOfWeek = ["Sunday", "Tuesday", "Thursday", "Friday", "Tuesday", "Sunday", "Tuesday", "Friday"];

daysOfWeek.indexOf("Tuesday", -3);

// The invocation above will return:
6

Try it on StackBlitz

Example 2: Find the index of Sunday from the 1st index position

const daysOfWeek = ["Sunday", "Tuesday", "Friday", "Sunday", "Tuesday", "Friday"];

daysOfWeek.indexOf("Sunday", 1);

// The invocation above will return:
3

Try it on StackBlitz

Suppose the startIndex argument is greater than or equal to the calling array's length. In such a case, the computer will ignore the search.

Here's an example:

const daysOfWeek = ["Sunday", "Tuesday", "Friday", "Sunday", "Tuesday", "Friday"];

daysOfWeek.indexOf("Sunday", 75);

// The invocation above will return:
-1

Try it on StackBlitz

tip

To find the last occurrence of a string, use lastIndexOf().

Overview

indexOf() searches its calling array for the first occurrence of the method's string argument.

If you like this article, please Tweet it.

Thanks for reading!