JavaScript TypedArray lastIndexOf() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
ES2015

Definition

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.

JavaScript
typedArray.lastIndexOf(searchElement)
typedArray.lastIndexOf(searchElement, fromIndex)
CallWhat it does
ta.lastIndexOf(0)Get the index of the last 0.
ta.lastIndexOf(5, 4)Start at index 4 and search toward index 0.
ta.lastIndexOf(5, -2)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

ParameterDescription
searchElementThe value to look for, compared with === against every stored element.
fromIndexOptional. 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 ValueDescription
NumberThe 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.

JavaScript
const packet = new Uint8Array([72, 105, 0, 33, 0, 90]);

const last = packet.lastIndexOf(0);
console.log(last);                              // 4
console.log(packet.slice(0, last).join(' '));   // 72 105 0 33
console.log(packet.slice(last + 1).join(' '));  // 90
console.log(packet.lastIndexOf(255));           // -1
Try It Yourself

How It Works

  1. Two zeros, the later one is chosen. The separator 0 sits at indexes 2 and 4. Searching from the end meets index 4 first.
  2. Cut before and after. slice(0, last) keeps the bytes before index 4, and slice(last + 1) keeps only the final byte 90.
  3. An absent byte gives -1. No element equals 255, so the backward walk reaches index 0 without a match and the answer is -1.

Example 2 — Where the Backward Search Starts

With fromIndex, the search begins at that position and moves toward index 0. Elements after it are never looked at.

JavaScript
const codes = new Uint8Array([5, 9, 5, 7, 9, 5]);

console.log(codes.lastIndexOf(5));       // 5
console.log(codes.lastIndexOf(5, 4));    // 2
console.log(codes.lastIndexOf(5, -2));   // 2
console.log(codes.lastIndexOf(5, 0));    // 0
console.log(codes.lastIndexOf(9, 3));    // 1
console.log(codes.lastIndexOf(7, 2));    // -1
console.log(codes.lastIndexOf(5, -10));  // -1
Try It Yourself

How It Works

  1. No fromIndex means the end. The search starts at index 5, where a 5 is stored, so the answer is 5 at once.
  2. 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.
  3. 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
Try It Yourself

How It Works

  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.
  2. 3 is not the string "3". Strict equality refuses to convert, so the string finds nothing. The number 3 matches the later index, 1.
  3. Floats need fround. The stored Float32 value is 0.10000000149011612. Math.fround(0.1) gives the same number, so index 1 is found.

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 Chrome All modern versions
Full support
Mozilla Firefox All modern versions
Full support
Apple Safari All modern versions
Full support
Microsoft Edge All modern versions
Full support
Node.js All current releases
Supported
Deno All releases
Supported
Internet Explorer IE 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.

Next: map()

Create a new typed array by running a function on every element.

map() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

11 people found this page helpful