typedArray.indexOf(value) returns the first index where the value is stored, or -1 if it is not there. It compares with strict equality (===), so no type conversion happens and NaN can never be found. This is the main difference from includes(), which does find NaN. An optional second argument sets the start position, and it may be negative.
Remember
typedArray.indexOf(value)
typedArray.indexOf(value, fromIndex)
new Uint8Array([5, 9, 5]).indexOf(5) → 0
new Uint8Array([5, 9, 5]).indexOf(5, 1) → 2
new Uint8Array([5, 9, 5]).indexOf(42) → -1
new Float32Array([NaN]).indexOf(NaN) → -1 (includes finds it)
Syntax
indexOf() takes the value to look for and an optional start position.
A negative start counts from the end: begin at length - 3.
Built into JavaScript since ES2015 on all 11 typed array types. The result is a plain number, even for BigInt typed arrays.
Library declarations
JavaScript
TypedArray.prototype.indexOf(searchElement, fromIndex)
// strict equality (===): no conversion, NaN is never found
// fromIndex < 0 -> start at length + fromIndex (not below 0)
// returns the first index, or -1
Parameters
Parameter
Description
searchElement
The value to look for, compared with === against every stored element. A string never matches a number.
fromIndex
Optional. Where to start. Negative values count from the end. The default is 0.
Return Value
Return Value
Description
Number
The first index where the value was found, or -1. The regular-array version is Array indexOf().
JavaScript Examples
Example 1 — First Match, Then Start Later
Five codes are stored in a Uint8Array with duplicates. We find the first match and then use fromIndex, including a negative one.
The first 9 wins.9 appears at indexes 1 and 4, but the search stops at the first one, index 1.
Starting at 2 skips it.indexOf(9, 2) ignores indexes 0 and 1, so the second 9 at index 4 is the one returned.
Negative starts and missing values.-3 becomes index 2, where a 5 waits. -1 starts at the last element, 9, so no 5 is found, and 42 is not stored anywhere.
Example 2 — indexOf() Cannot Find NaN
A Float64Array stores NaN for a missing measurement. Three ways to search show which one works.
0.1 is not stored as 0.1. The Float32 element is 0.10000000149011612, so searching for the double 0.1 fails with -1.
fround repairs the match.Math.fround(0.1) rounds to single precision first. 0.5 needs no help because it is exact in binary.
Strings and negative zero. The string "7" is not the number 7. And -0 === 0 is true, so searching for -0 finds the 0 at index 1.
Compatibility
Browser & Runtime Support
TypedArray.prototype.indexOf() arrived with typed arrays and Array.prototype.indexOf() in ES2015 (ES6).
✓ Baseline · ES2015
TypedArray.prototype.indexOf()
Supported in every modern browser, plus Node.js, Deno, and Bun.
98%Modern browser support
Google ChromeAll modern versions
Full support
Mozilla FirefoxAll modern versions
Full support
Apple SafariAll modern versions
Full support
Microsoft EdgeAll modern versions
Full support
Node.jsAll current releases
Supported
DenoAll releases
Supported
Internet ExplorerIE 10 and 11 have typed arrays, but not this method
Not supported
indexOf()Excellent
Bottom line: Safe everywhere modern JavaScript runs. Only Internet Explorer is missing it: IE 10 and 11 have typed arrays, but not this method.
Frequently Asked Questions
The first index where the value is stored, or -1 if it is not found. It never throws for a missing value.
indexOf() uses strict equality, and NaN === NaN is false. Use includes(NaN) to check, or findIndex(Number.isNaN) to get the position.
It counts from the end: the start index is length + fromIndex. In a typed array of length 5, -3 starts at index 2. If that is still below 0, the search starts at 0.
No. It uses ===, so the string "7" does not match the number 7. Searching for -0 does match 0, because -0 === 0.
indexOf() searches from the start and returns the first match. lastIndexOf() searches from the end and returns the last match.