JavaScript TypedArray indexOf() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
ES2015

Definition

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.

JavaScript
typedArray.indexOf(searchElement)
typedArray.indexOf(searchElement, fromIndex)
CallWhat it does
ta.indexOf(9)Get the index of the first 9.
ta.indexOf(9, 2)Skip indexes 0 and 1, then search from index 2.
ta.indexOf(5, -3)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

ParameterDescription
searchElementThe value to look for, compared with === against every stored element. A string never matches a number.
fromIndexOptional. Where to start. Negative values count from the end. The default is 0.

Return Value

Return ValueDescription
NumberThe 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.

JavaScript
const codes = new Uint8Array([5, 9, 5, 7, 9]);

console.log(codes.indexOf(9));      // 1
console.log(codes.indexOf(9, 2));   // 4
console.log(codes.indexOf(5, -3));  // 2
console.log(codes.indexOf(5, -1));  // -1
console.log(codes.indexOf(42));     // -1
Try It Yourself

How It Works

  1. The first 9 wins. 9 appears at indexes 1 and 4, but the search stops at the first one, index 1.
  2. Starting at 2 skips it. indexOf(9, 2) ignores indexes 0 and 1, so the second 9 at index 4 is the one returned.
  3. 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.

JavaScript
const values = new Float64Array([NaN, 1]);

console.log(values.indexOf(NaN));                   // -1
console.log(values.includes(NaN));                  // true
console.log(values.findIndex((v) => Number.isNaN(v))); // 0
console.log(values.indexOf(1));                     // 1
Try It Yourself

How It Works

  1. Strict equality says no. NaN === NaN is false, so indexOf(NaN) reports -1 even though the first element is NaN.
  2. includes() says yes. It uses a rule where NaN equals NaN, so it returns true, but it cannot tell you where.
  3. findIndex() gives the position. The test Number.isNaN(v) accepts index 0. Normal numbers such as 1 work with indexOf() as expected.

Example 3 — Types and Rounding Matter

No conversion is applied to the search value. A string fails, and a Float32Array needs a rounded search value.

JavaScript
const f = new Float32Array([0.1, 0.5]);
console.log(f.indexOf(0.1));                // -1
console.log(f.indexOf(Math.fround(0.1)));   // 0
console.log(f.indexOf(0.5));                // 1

const ints = new Int16Array([7, 0]);
console.log(ints.indexOf('7'));             // -1
console.log(ints.indexOf(7));               // 0
console.log(ints.indexOf(-0));              // 1
Try It Yourself

How It Works

  1. 0.1 is not stored as 0.1. The Float32 element is 0.10000000149011612, so searching for the double 0.1 fails with -1.
  2. fround repairs the match. Math.fround(0.1) rounds to single precision first. 0.5 needs no help because it is exact in binary.
  3. 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.

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 Chrome All modern versions
Full support
Mozilla Firefox All modern versions
Full support
Apple Safari All modern versions
Full support
Microsoft Edge All modern versions
Full support
Node.js All current releases
Supported
Deno All releases
Supported
Internet Explorer IE 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.

Next: join()

Join the elements of the typed array into one string.

join() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

11 people found this page helpful