typedArray.findLast(callback) works like find(), but it starts at the last element and moves toward the first. It returns the first element it meets that passes the test, which is the last match in normal order. If none passes, it returns undefined. It is useful when the typed array has duplicates or a history of values and you want the newest one. The method is part of ES2023.
The callback also gets the index and the typed array. The indexes go down.
ta.findLast(function (v) { ... }, obj)
Use obj as this inside a regular function.
ES2023 method on all 11 typed array types. The callback is first called for index length - 1, then length - 2, and so on.
Library declarations
JavaScript
TypedArray.prototype.findLast(callbackFn, thisArg)
// callbackFn(value, index, array), called from the last index down to 0
// returns the element, or undefined if none passed
Parameters
Parameter
Description
callbackFn
A function called as (value, index, array). Return a truthy value to pick the element. The index is still the real position, not a count from the end.
thisArg
Optional. The value used as this inside callbackFn (not for arrow functions).
Return Value
Return Value
Description
Number, BigInt, or undefined
The last element that passed the test, or undefined. See Array findLast() for the regular-array version.
JavaScript Examples
Example 1 — Skip the Error Markers at the End
A sensor writes 255 into a Uint8Array when it fails. To get the latest valid reading we search from the end.
JavaScript
const readings = new Uint8Array([20, 22, 255, 24, 255, 255]);
console.log(readings.findLast((v) => v !== 255)); // 24
console.log(readings.findLast((v) => v === 255)); // 255
console.log('none: ' + readings.findLast((v) => v > 300)); // none: undefined