Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

javascript

What is array indexOf() in Javascript?

Samia Ishaque

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);

RELATED TAGS

javascript

CONTRIBUTOR

Samia Ishaque
Copyright ©2022 Educative, Inc. All rights reserved
RELATED COURSES

View all Courses

Keep Exploring