android-logo

JavaScript Array indexOf method


The indexOf() function returns the first occurrence of a given value in a string. If the supplied value cannot be found, it returns -1.

Syntax


string.indexOf(searchvalue, start)

Using the JavaScript indexOf method on strings


let str = 'Welcome to Androidaura tutorials';
let index = str.indexOf('Androidaura');
        
console.log(index); // 11

Using the JavaScript indexOf method on Array

You may use the indexOf method of an array if the item is a primitive value, such as a string or number.


const alphabets = ['a', 'b', 'c']
const index = alphabets.indexOf('c')

console.log(index); // 2

Case Sensitive


let str = 'hello world';
let index = str.indexOf("World");
        
console.log(index); // -1

If the supplied value not found, indexOf function returns -1 value.