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);
New on Educative
Learn to Code
Learn any Language as a beginner
Develop a human edge in an AI powered world and learn to code with AI from our beginner friendly catalog
🏆 Leaderboard
Daily Coding Challenge
Solve a new coding challenge every day and climb the leaderboard

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved