Array.prototype.indexOf(value) returns the first index where value appears. If it is missing, you get -1. Need only yes/no? Prefer includes(). Need the last match? Use lastIndexOf().
Remember
array.indexOf(value)
array.indexOf(value, fromIndex)
[1, 2, 3, 2].indexOf(2) → 1
[1, 2, 3, 2].indexOf(9) → -1
[NaN].indexOf(NaN) → -1 // use includes for NaN
compares with ===
does not mutate the array
check with !== -1 (index 0 is valid!)
Syntax
Call array.indexOf. Start with this line.
JavaScript
array.indexOf(value, fromIndex = 0)
Call
What it does
array.indexOf(value, fromIndex = 0)
Primary signature.
array.indexOf(...)
Call with the arguments your case needs.
Built into JavaScript. Call indexOf(...) on an array instance — no import required.
Library declarations
JavaScript
array.indexOf(value, fromIndex = 0)
Parameters
Parameter
Description
Array
Required. Source to search
value
Required. Value to look for
fromIndex
Optional. Start index; negative counts from the end
Return Value
Return Value
Description
*
Index (≥ 0) or -1
JavaScript Examples
Example 1 — Find the First Index
Get the position of a value, or -1 when it is missing.
Normal values. Both see 1; indexOf also gives index 0.
indexOf misses NaN. Because NaN === NaN is false.
includes finds NaN. SameValueZero treats NaN as equal to itself.
Pick by need. Index → indexOf; yes/no (or NaN) → includes.
Compatibility
Browser & Runtime Support
Array.prototype.indexOf() is part of ES5. It has been available in browsers and Node.js for a long time.
✓ Baseline · ES5
Array.prototype.indexOf()
Supported in all modern browsers, Internet Explorer 9+, and every current Node.js release.
99%Browser 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 almost everywhere. Prefer includes() for clearer membership checks in modern code, but indexOf() remains the right tool when you need the index.
Frequently Asked Questions
indexOf() returns the index of the first matching element in the array. If the value is not found, it returns -1.
indexOf() returns a number (index or -1). includes() returns true or false. For simple membership checks, includes() is clearer and also finds NaN correctly.
It returns -1. Always compare with !== -1 (or use includes() for a boolean). Do not use if (arr.indexOf(x)) because index 0 is falsy.
Yes. It compares with ===. The number 2 and the string "2" are different. Also, NaN === NaN is false, so indexOf(NaN) is always -1.
fromIndex is optional. It sets where the search starts. Negative values count from the end of the array.
No. indexOf() only reads the array and returns a number. The original array is unchanged.