TypedArray.prototype.indexOf()
En esta página
The indexOf() method of {{jsxref("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 {{jsxref("Array.prototype.indexOf()")}}.
{{InteractiveExample("JavaScript Demo: TypedArray.prototype.indexOf()")}}
```js interactive-example 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
## Syntax
```js-nolint
indexOf(searchElement)
indexOf(searchElement, fromIndex)
Parameters#
searchElement- : Element to locate in the typed array.
fromIndex{{optional_inline}}- : 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 {{jsxref("Array.prototype.indexOf()")}} for more details. This method is not generic and can only be called on typed array instances.
Examples#
Using indexOf()#
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#
{{Specifications}}
Browser compatibility#
{{Compat}}
See also#
- Polyfill of
TypedArray.prototype.indexOfincore-js - JavaScript typed arrays guide
- {{jsxref("TypedArray")}}
- {{jsxref("TypedArray.prototype.findIndex()")}}
- {{jsxref("TypedArray.prototype.findLastIndex()")}}
- {{jsxref("TypedArray.prototype.lastIndexOf()")}}
- {{jsxref("Array.prototype.indexOf()")}}
- {{jsxref("String.prototype.indexOf()")}}