The indexOf() method returns the first index where a value appears, or -1 if it is missing. Optional fromIndex controls where the search starts. This tutorial covers syntax, five examples, strict equality, and how indexOf() compares to includes() and lastIndexOf().
01
Syntax
indexOf(val)
02
First match
Left to right
03
Not found
-1
04
Strict
===
05
fromIndex
Start offset
06
ES5
Wide support
Fundamentals
Introduction
When you need the position of a value—not just whether it exists—indexOf() is the classic array search method. It scans from the start (or from fromIndex) and stops at the first match.
For a simple yes/no check, modern code often prefers includes(). Use indexOf() when the index itself matters: splicing, inserting nearby, or deduplicating.
Concept
Understanding the indexOf() Method
array.indexOf(searchElement, fromIndex?) compares each element with strict equality (===). The return value is a zero-based index or -1.
Duplicate values are common. indexOf always returns the first occurrence. To find the last match, use lastIndexOf().
💡
Beginner Tip
Index 0 is valid, so never test with if (index). Use index !== -1 or includes() instead.
Foundation
📝 Syntax
General form of Array.prototype.indexOf:
JavaScript
array.indexOf(searchElement, fromIndex)
Parameters
searchElement — value to locate.
fromIndex — optional; index to start searching (default 0).
Return value
First matching index (0 or greater).
-1 if no match.
Original array unchanged.
Common patterns
arr.indexOf("blue") — find first index.
arr.indexOf("blue", 2) — search from index 2.
arr.indexOf(x) !== -1 — membership test (prefer includes).
arr.filter((v, i, a) => a.indexOf(v) === i) — dedupe pattern.
Cheat Sheet
⚡ Quick Reference
Goal
Code
First index of value
arr.indexOf(value)
Search from index
arr.indexOf(value, fromIndex)
Not found
-1
Boolean check
arr.includes(value)
Last occurrence
arr.lastIndexOf(value)
Compare
📋 indexOf() vs includes() vs lastIndexOf() vs findIndex()
Choose based on whether you need an index, a boolean, direction, or a custom test.
indexOf
first index
Exact value
includes
true / false
Membership
lastIndexOf
last index
Right to left
findIndex
predicate
Custom test
Hands-On
Examples Gallery
Open DevTools Console (F12) or use Try-it labs. Example N maps to ?tryit=N.
📚 Getting Started
Locate values and handle missing items.
Example 1 — Find the Index of a Value
Search a color array for the first occurrence of "green".
An item is kept only when its index equals the first index of that value. Modern code often uses [...new Set(arr)] instead, but this pattern teaches how indexOf works.
Applications
🚀 Common Use Cases
Find position — locate an item before splice or insert.
Membership via index — indexOf(x) !== -1 (or use includes).
Deduplication — filter where indexOf(v) === i.
Skip processed items — pass fromIndex to find next duplicate.
Simple lookups — tags, enums, or menu item positions.
Returns first match only—use lastIndexOf for last.
Not found → -1 (index 0 is valid).
Strict ===; does not find NaN.
Objects match by reference, not deep equality.
For boolean checks, prefer includes() in modern code.
ES5—excellent support including IE9+.
Compatibility
Browser & Runtime Support
Array.prototype.indexOf() has been available since ES5 (2009). It is one of the oldest and most widely supported array search methods.
✓ Baseline · ES5
Array.prototype.indexOf()
Supported in Chrome 1+, Firefox 1.5+, Safari 3+, Edge (all versions), IE 9+, and all modern Node.js versions.
99%Universal support
Google ChromeSupported · Desktop & Mobile
Full support
Mozilla FirefoxSupported · Desktop & Mobile
Full support
Apple SafariSupported · macOS & iOS
Full support
Microsoft EdgeSupported · Chromium
Full support
Internet ExplorerNo native support · Use a polyfill
Polyfill
OperaSupported · Modern versions
Full support
Samsung InternetSupported · Android
Full support
BunSupported · JavaScript runtime
Supported
DenoSupported · JavaScript runtime
Supported
Node.jsSupported · Server runtime
Supported
Android WebViewSupported · Modern WebView
Full support
Array.indexOf()Excellent
Bottom line: Safe to use everywhere except very old IE8 environments. No polyfill needed for modern projects.
Wrap Up
Conclusion
indexOf() answers “where is the first occurrence?” with an index or -1. Pair it with careful !== -1 checks, or use includes() when you only need true/false.
Next, learn join() to combine array elements into a single string.