MeshWorld India LogoMeshWorld.

indexOf() Method in Javascript

Listen to ArticleAI Speech
~1 min read narration
100%
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.

Reader Quality Feedback

Did this technical guide help solve your problem?

Suggest Errata ($0)
Parimal Ghadiyali
Primary Author

Parimal Ghadiyali

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

Explore Author Archive
Compute Fuel & Open Testbed
100% Independent & Verified

Fuel High-Density, Zero-Fluff Engineering Deep-Dives

Every guide on MeshWorld is validated on physical Linux nodes and reproducible testbeds. If this article saved you hours of debugging or unblocked production, consider funding our next cluster run.

Weekly Dispatch

Join MeshWorld Dispatch

Get practical tutorials, system blueprints, and curated AI engineering notes straight to your inbox. No fluff, zero spam.

Zero spam. 1-click unsubscribe anytime.Prefer RSS?
Curated Continuations

Up Next in This Domain.

Browse Full Archive