What is array indexOf() in Javascript?

Overview

The indexOf function in Javascript is used to find the index of a particular key in the array. The general syntax for indexOf is:

array.indexOf(keyName);

Parameters

The function indexOf takes one single mandatory input as an argument. The mandatory input is the name of the key we want the index of.

Return value

The function indexOf returns the index of the key.

Example

The following example will help you understand the function indexOf more clearly. First, we create an array of names. Then, we use the function indexOf to find the index of the name “Sana.” Lastly, we print the index.

const names = ["Samia", "Sana", "Ali"];
const ind = names.indexOf("Sana");
console.log(ind);

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved