JavaScript Tutorials

JavaScript Array indexOf()
Syntax & Examples

Array.indexOf() method

The indexOf() method of the Array class in JavaScript returns the first (least) index at which a given element can be found in the calling array. If the element is not found, it returns -1.


Syntax of Array.indexOf()

There are 2 variations for the syntax of Array.indexOf() method. They are:

1.
indexOf(searchElement)

Parameters

ParameterOptional/RequiredDescription
searchElementrequiredThe element to locate in the array.

This method returns the first (least) index at which a given element can be found in the calling array, starting the search at the beginning of the array.

Returns value of type Number.

2.
indexOf(searchElement, fromIndex)

Parameters

ParameterOptional/RequiredDescription
searchElementrequiredThe element to locate in the array.
fromIndexoptionalThe index to start the search at. Defaults to 0.

This method returns the first (least) index at which a given element can be found in the calling array, starting the search at the specified fromIndex.

Returns value of type Number.



✐ Examples

1 Using indexOf() method to find the index of an element

In JavaScript, we can use the indexOf() method to find the index of a specified element in an array.

For example,

  1. We define an array variable arr with elements [1, 2, 3, 4, 5].
  2. We use the indexOf() method with the value 3 to find the index of the element 3.
  3. The result is stored in the variable indexOfThree.
  4. We log indexOfThree to the console using console.log() method to see the index of the element 3.

JavaScript Program

const arr = [1, 2, 3, 4, 5];
const indexOfThree = arr.indexOf(3);
console.log(indexOfThree);

Output

2

2 Using indexOf() method to find the index of an element starting from a specific index

We can use the indexOf() method to find the index of a specified element in an array, starting the search from a specific index.

For example,

  1. We define an array variable arr with elements [1, 2, 3, 4, 5, 3].
  2. We use the indexOf() method with the value 3 and the fromIndex 3 to find the index of the element 3 starting from index 3.
  3. The result is stored in the variable indexOfThreeFromIndex3.
  4. We log indexOfThreeFromIndex3 to the console using console.log() method to see the index of the element 3 starting from index 3.

JavaScript Program

const arr = [1, 2, 3, 4, 5, 3];
const indexOfThreeFromIndex3 = arr.indexOf(3, 3);
console.log(indexOfThreeFromIndex3);

Output

5

3 Using indexOf() method to find the index of a string in an array

We can use the indexOf() method to find the index of a specified string in an array.

For example,

  1. We define an array variable strArr with elements ['apple', 'banana', 'cherry'].
  2. We use the indexOf() method with the value 'banana' to find the index of the string 'banana'.
  3. The result is stored in the variable indexOfBanana.
  4. We log indexOfBanana to the console using console.log() method to see the index of the string 'banana'.

JavaScript Program

const strArr = ['apple', 'banana', 'cherry'];
const indexOfBanana = strArr.indexOf('banana');
console.log(indexOfBanana);

Output

1

Summary

In this JavaScript tutorial, we learned about indexOf() method of Array: the syntax and few working examples with output and detailed explanation for each example.