typedArray.findLastIndex(callback) searches from the end toward the start and returns the index of the first element it meets that passes your test. In normal order that is the last match. If no element passes, it returns -1. It is the position-based partner of findLast() and was added in ES2023.
The callback also gets the index and the typed array. The indexes go down.
ta.subarray(0, i + 1)
A common follow-up: keep everything up to and including the match.
ES2023 method on all 11 typed array types. The callback is called from index length - 1 down to 0, and the result is a plain number.
Library declarations
JavaScript
TypedArray.prototype.findLastIndex(callbackFn, thisArg)
// callbackFn(value, index, array), called from the last index down to 0
// returns the index of the last passing element, or -1
Parameters
Parameter
Description
callbackFn
A function called as (value, index, array). Return a truthy value for the element you want.
thisArg
Optional. The value used as this inside callbackFn (not for arrow functions).
Return Value
Return Value
Description
Number
The index of the last element that passed, or -1 if none did. The regular-array twin is Array findLastIndex().
JavaScript Examples
Example 1 — Last Negative Value
An Int16Array holds a few signed readings. We compare the first and the last position of a negative one.
JavaScript
const readings = new Int16Array([4, -2, 9, -7, 3]);
console.log(readings.findIndex((v) => v < 0)); // 1
console.log(readings.findLastIndex((v) => v < 0)); // 3
const last = readings.findLastIndex((v) => v < 0);
console.log('value there: ' + readings[last]); // value there: -7