Movatterモバイル変換


[0]ホーム

URL:


  1. Web
  2. JavaScript
  3. Reference
  4. Standard built-in objects
  5. TypedArray
  6. indexOf()

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⁩.

TheindexOf() method ofTypedArray 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 asArray.prototype.indexOf().

Try it

const uint8 = new Uint8Array([10, 20, 30, 40, 50]);console.log(uint8.indexOf(50));// Expected output: 4// From position 3console.log(uint8.indexOf(20, 3));// Expected output: -1console.log(uint8.indexOf(51));// Expected output: -1

Syntax

js
indexOf(searchElement)indexOf(searchElement, fromIndex)

Parameters

searchElement

Element to locate in the typed array.

fromIndexOptional

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

Return value

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

Description

SeeArray.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); // 0uint8.indexOf(7); // -1uint8.indexOf(9, 2); // 2uint8.indexOf(2, -1); // -1uint8.indexOf(2, -3); // 0

Specifications

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

Browser compatibility

See also

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp