TypedArray.prototype.indexOf()

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since September 2016.

The indexOf() method of TypedArray instances returns the first index at which a given element can be found in the typed array, or -1 if it is not present. This method has the same algorithm as Array.prototype.indexOf().

Try it

Syntax

js
indexOf(searchElement)
indexOf(searchElement, fromIndex)

Parameters

searchElement

Element to locate in the typed array.

fromIndex Optional

Zero-based index at which to start searching, converted to an integer.

Return value

The first index of searchElement in the typed array; -1 if not found.

Description

See Array.prototype.indexOf() for more details. This method is not generic and can only be called on typed array instances.

Examples

Using indexOf()

js
const uint8 = new Uint8Array([2, 5, 9]);
uint8.indexOf(2); // 0
uint8.indexOf(7); // -1
uint8.indexOf(9, 2); // 2
uint8.indexOf(2, -1); // -1
uint8.indexOf(2, -3); // 0

Specifications

Specification
ECMAScript Language Specification
# sec-%typedarray%.prototype.indexof

Browser compatibility

BCD tables only load in the browser

See also