Typed Array indexOf()
Example
// Create a Typed Array
const myArr = Int16Array.from([10,15,20,25,30,35,40,45,50]);
// The index of the first element with a value of 40
let result = myArr.indexOf(40);
Try it Yourself »
Description
The indexOf()
method returns the first index (position) of a specified value.
The indexOf()
method returns -1 if the value is not found.
The indexOf()
method starts at a specified index and searches from left to right
(from the given start postion to the end of the array).
By default the search starts at the first element and ends at the last.
Negative start values counts from the last element (but still searches from left to right).
Syntax
type-array.indexOf(item, start)
typed-array must be one of the following: Int8ArrayUint8Array Uint8ClampedArray Int16Array Uint16Array Int32Array Uint32Array Float16Array Float32Array Float64Array BigInt64Array BigUint64Array |
Parameters
Parameter | Description | |
item | Required. The value to search for. |
|
start | Optional. Where to start the search. Default value is 0. Negative values start the search from the end of the array. |
Return Value
Type | Description |
A number | The index (position) of the first item found. -1 if the item is not found. |
Typed Array Find Methods:
Method | Finds |
---|---|
includes() | Returns true if an element is present in the array |
indexOf() | The index of the first element with a specified value |
lastIndexOf() | The index of the last element with a specified value |
find() | The value of the first element that passes a test |
findIndex() | The index of the first element that passes a test |
findLast() | The value of the last element that passes a test |
findLastIndex() | The index of the last element that passes a test |
Note
In an array, the first element has index (position) 0, the second has index 1, ...
More Examples
Find the first index of "Apple", starting from the last element:
const fruits = ["Banana", "Orange", "Apple", "Mango", "Apple"];
let index = fruits.indexOf("Apple", -1);
Try it Yourself »
JavaScript Typed Arrays
Browser Support
typed-array.indexOf()
is an ECMAScript6 (ES6 2015) feature.
JavaScript 2015 is fully supported in all modern browsers since June 2017:
Chrome 51 | Edge 15 | Firefox 54 | Safari 10 | Opera 38 |
May 2016 | Apr 2017 | Jun 2017 | Sep 2016 | Jun 2016 |