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);
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.
The function indexOf
returns the index of the key.
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