indexOf() Method in Javascript

ES5 Specifications includes the indexOf() method for Array data structure.

The indexOf() method i used to determines whether an array contains a specific element or not. It returns its position if it exists in an array.

Syntax

array.indexOf(item, start);

Example 1

const array = ["Red", "Orange", "Yellow", "Olive", "Green"];

console.log(array.indexOf("Red")); // Output: 0
console.log(array.indexOf("Pink")); // Output: -1

Example 2

const array = ["Red", "Orange", "Yellow", "Olive", "Green", "Red"];

console.log(array.indexOf("Red", 2)); // Output: 5
console.log(array.indexOf("Pink")); // Output: -1

Hope you find this helpful.

Parimal Ghadiyali
Parimal Ghadiyali

A web geek, an industry experienced web developer & tutor/instructor residing in India 🇮🇳