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.

indexOf()TypedArray インスタンスのメソッドで、指定された要素が型付き配列内で見つかった最初のインデックスを返し、存在しなければ -1 を返します。このメソッドは Array.prototype.indexOf() と同じアルゴリズムです。

試してみましょう

const uint8 = new Uint8Array([10, 20, 30, 40, 50]);

console.log(uint8.indexOf(50));
// Expected output: 4

// From position 3
console.log(uint8.indexOf(20, 3));
// Expected output: -1

console.log(uint8.indexOf(51));
// Expected output: -1

構文

js
indexOf(searchElement)
indexOf(searchElement, fromIndex)

引数

searchElement

型付き配列内を検索する要素。

fromIndex 省略可

検索を始める位置を示すゼロ基点のインデックスで、整数に変換されます

返値

型付き配列内の最初の searchElement のインデックスです。見つからなかったら -1 になります。

解説

詳細については、 Array.prototype.indexOf() をご覧ください。このメソッドは汎用的ではなく、型付き配列インスタンスに対してのみ呼び出すことができます。

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

仕様書

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

ブラウザーの互換性

関連情報