typedArray.lastIndexOf(value) searches backward, from the last element toward the first, and returns the last index where the value is stored. If the value is not found it returns -1. It uses strict equality, so there is no type conversion and NaN is never found. An optional fromIndex tells it where to start the backward search.
Remember
typedArray.lastIndexOf(value)
typedArray.lastIndexOf(value, fromIndex)
Uint8Array [5, 9, 5, 7, 9, 5]
.lastIndexOf(5) → 5
.lastIndexOf(5, 4) → 2 (start at index 4, go left)
.lastIndexOf(5, -2) → 2 (-2 means index 4)
.lastIndexOf(42) → -1
Syntax
lastIndexOf() takes the value to look for and an optional start position.
A negative start counts from the end: begin at length - 2.
Built into JavaScript since ES2015 on all 11 typed array types. The default fromIndex is length - 1, the last element.
Library declarations
JavaScript
TypedArray.prototype.lastIndexOf(searchElement, fromIndex)
// strict equality (===): no conversion, NaN is never found
// searches from fromIndex down to 0 (default: length - 1)
// fromIndex < 0 -> start at length + fromIndex
// returns the last index, or -1
Parameters
Parameter
Description
searchElement
The value to look for, compared with === against every stored element.
fromIndex
Optional. The index where the backward search begins. Elements after it are ignored. Negative values count from the end. The default is length - 1.
Return Value
Return Value
Description
Number
The last index at or before fromIndex where the value was found, or -1. The regular-array version is Array lastIndexOf().
JavaScript Examples
Example 1 — Split a Packet at the Last Separator
A Uint8Array packet uses 0 as a separator byte. lastIndexOf() finds the final one, so we can cut the packet in two parts.
No fromIndex means the end. The search starts at index 5, where a 5 is stored, so the answer is 5 at once.
A start of 4 skips index 5. Beginning at index 4 (9), the walk goes left and finds 5 at index 2. The same happens for -2, because 6 - 2 = 4.
Early starts see less.(5, 0) only checks index 0. (7, 2) misses the 7 at index 3, which is after the start. -10 lands before index 0, so nothing is searched.
Example 3 — NaN, Strings, and Rounded Floats
Like indexOf(), this method never converts the search value and never finds NaN.
JavaScript
const f = new Float32Array([0.5, NaN, 0.5]);
console.log(f.lastIndexOf(NaN)); // -1
console.log(f.lastIndexOf(0.5)); // 2
const ints = new Int32Array([3, 3]);
console.log(ints.lastIndexOf('3')); // -1
console.log(ints.lastIndexOf(3)); // 1
const g = new Float32Array([0.1, 0.1]);
console.log(g.lastIndexOf(0.1)); // -1
console.log(g.lastIndexOf(Math.fround(0.1))); // 1
NaN stays hidden. The middle element is NaN, but NaN === NaN is false, so lastIndexOf(NaN) is -1. Use includes(NaN) to detect it.
3 is not the string "3". Strict equality refuses to convert, so the string finds nothing. The number 3 matches the later index, 1.
Floats need fround. The stored Float32 value is 0.10000000149011612. Math.fround(0.1) gives the same number, so index 1 is found.
Compatibility
Browser & Runtime Support
TypedArray.prototype.lastIndexOf() arrived with typed arrays and Array.prototype.lastIndexOf() in ES2015 (ES6).
✓ Baseline · ES2015
TypedArray.prototype.lastIndexOf()
Supported in every modern browser, plus Node.js, Deno, and Bun.
98%Modern browser support
Google ChromeAll modern versions
Full support
Mozilla FirefoxAll modern versions
Full support
Apple SafariAll modern versions
Full support
Microsoft EdgeAll modern versions
Full support
Node.jsAll current releases
Supported
DenoAll releases
Supported
Internet ExplorerIE 10 and 11 have typed arrays, but not this method
Not supported
lastIndexOf()Excellent
Bottom line: Safe everywhere modern JavaScript runs. Only Internet Explorer is missing it: IE 10 and 11 have typed arrays, but not this method.
Frequently Asked Questions
The last index where the value is stored, found by searching backward from the end. It returns -1 if the value is not found.
It is the index where the backward search starts. Only elements at that index or before it are checked. The default is length - 1.
It counts from the end: the start index is length + fromIndex. For length 6, -2 starts at index 4. Below -length, nothing is searched and the result is -1.
No. It uses strict equality, so NaN is never found and the result is -1. includes(NaN) can find it.
lastIndexOf() looks for one exact value. findLastIndex() calls your function on each element, so it can search with any condition.