MeshWorld India LogoMeshWorld.

indexOf() Method in Javascript

Parimal Ghadiyali
By Parimal Ghadiyali
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.

Share_This Twitter / X
Parimal Ghadiyali
Written By

Parimal Ghadiyali

A web geek, industry-experienced web developer & instructor residing in India ๐Ÿ‡ฎ๐Ÿ‡ณ. Passionate about learning new things and making things on the Internet.

Enjoyed this article?

Support MeshWorld and help us create more technical content