You can loop backward:

const aLastIndexOf = (str, ch) => {
    for (let index = str.length - 1; index >= 0; index--) {
        if (str[index] == ch)
            return index;
    }
    return -1;
}

An example:

const aLastIndexOf = (str, ch) => {
    for (let index = str.length - 1; index >= 0; index--) {
        if (str[index] == ch)
            return index;
    }
    return -1;
}

console.log(aLastIndexOf("hello", 'h'));
console.log(aLastIndexOf("hello", 'l'));
console.log(aLastIndexOf("hello", 'e'));

Answer from StepUp on Stack Overflow
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › String › lastIndexOf
String.prototype.lastIndexOf() - JavaScript | MDN
The lastIndexOf() method of String values searches this string and returns the index of the last occurrence of the specified substring. It takes an optional starting position and returns the last occurrence of the specified substring at an index less than or equal to the specified number.
🌐
W3Schools
w3schools.com › jsref › jsref_lastindexof.asp
JavaScript String lastIndexOf() Method
The lastIndexOf() method returns the index (position) of the last occurrence of a specified value in a string.
Discussions

javascript - Alternative to the lastIndexOf() function - Stack Overflow
Here is the task at hand: "Write a function called stringLastIndexOf, which accepts two strings: the first is a word and the second is a single character. The function should return the last ... More on stackoverflow.com
🌐 stackoverflow.com
Slice/lastIndexOf method in javascript
Ok, lets start with 'lastIndexOf'. Imagine this const array = ['a', 'a', 'b', 'b', 'c', 'c', 'c'] Now what they ask you to do is tell them where in that array the last 'b' is located. So you do array.lastIndexOf('b') Which will give you the number 3, since the first position is 0. Now you can work with that information. Lets say you want to clear all the old data. The new data is the fancy 'c' the client wants, everything else is to be ignored. Slice takes two arguments, start of what you want to extract and end, but end is optional, which means you can just use the start. So if you know that the 'c' starts after 'b', your start will be the position after 'b'. lastIndexOf tells you where 'b' is, so you can combine them like this: const newArray = array.slice(array.lastIndexOf('b') + 1) This will return only the three 'c's. If you had used slice(2, 4), it would start at the 3rd element, which is a 'b', include the 4th element, also a 'b' and end at the 5th element (index 4). So it would ignore the rest and the new array would be ['b', 'b', 'c']. I'm on the phone in the train, so this could be poorly formatted and maybe not well explained, please let me know if so, I'll try again. More on reddit.com
🌐 r/learnjavascript
5
1
August 28, 2023
javascript - Find the lastIndexOf() an object with a key in array of objects - Stack Overflow
We can make lastIndexOf work, we just need to make the value comparable (strict equality conform). Or simply put: we need to map the objects to a single property that we want to find the last index of using javascript's native implementation. More on stackoverflow.com
🌐 stackoverflow.com
string.lastIndexOf()
Think of it like this: ('abc' + '').lastIndexOf('') // 3 Therefore: 'abc'.lastIndexOf('') // 3 Similarly: ('' + 'abc').indexOf('') // 0 So: 'abc'.indexOf('') // 0 Returning the length of the string was a design decision, but in the universe of possible return values (0, -1, length of the string, null, undefined, Error, etc.) it's a decision that makes at least some sense, and more importantly it is internally consistent with other JS APIs. More on reddit.com
🌐 r/learnjavascript
5
13
July 31, 2017
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Array › lastIndexOf
Array.prototype.lastIndexOf() - JavaScript | MDN
The lastIndexOf() method of Array instances returns the last index at which a given element can be found in the array, or -1 if it is not present. The array is searched backwards, starting at fromIndex.
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › javascript-string-lastindexof-method
JavaScript String lastIndexOf() Method - GeeksforGeeks
July 16, 2024 - It then uses the lastIndexOf() method to find the index of the last occurrence of the substring 'train' (case-sensitive) within the string str. Since 'train' is not found in 'Departed Train', -1 is printed to the console.
🌐
Programiz
programiz.com › javascript › library › string › lastindexof
JavaScript String lastIndexOf() (With Examples)
Become a certified JavaScript programmer. Try Programiz PRO! ... The lastIndexOf() method returns the last index of occurence of a given substring in the string.
🌐
Codecademy
codecademy.com › docs › javascript › strings › .lastindexof()
JavaScript | Strings | .lastIndexOf() | Codecademy
November 24, 2025 - The .lastIndexOf() method in JavaScript returns the position of the last occurrence of a specified substring within a string. If the substring is not found, it returns -1.
Find elsewhere
🌐
Medium
medium.com › @amirakhaled2027 › demystifying-lastindexof-and-indexof-mastering-string-search-in-javascript-bd06d9c7429d
Demystifying lastIndexOf and indexOf: Mastering String Search in JavaScript | by Amira Khaled | Medium
May 27, 2024 - In the realm of JavaScript development, efficiently searching within strings is a fundamental task. The built-in methods lastIndexOf and indexOf provide powerful tools for locating the position of a substring within a string, but understanding their subtle differences is crucial for accurate and efficient code.
🌐
Medium
medium.com › nerd-for-tech › basics-of-javascript-string-lastindexof-method-2cfe3de6ce33
Basics of Javascript · String · lastIndexOf() (method) | by Jakub Korch | Nerd For Tech | Medium
June 7, 2021 - The lastIndexOf() method returns the index of the last occurrence of the specified string value within the searched string. Search happens backwards by default starting at the last index of the searched string moving towards the index 0. Optionally ...
🌐
Vultr Docs
docs.vultr.com › javascript › standard-library › String › lastIndexOf
JavaScript String lastIndexOf() - Find Last Index | Vultr Docs
May 15, 2025 - The lastIndexOf() method in JavaScript is a string method used to determine the last occurrence of a specified value within a string. This method can search for both strings and characters, which makes it a versatile tool in text processing.
🌐
Reddit
reddit.com › r/learnjavascript › slice/lastindexof method in javascript
r/learnjavascript on Reddit: Slice/lastIndexOf method in javascript
August 28, 2023 -

Can anyone explain what slice and lastIndexOf does in JavaScript. I searched it up but the explanation is too hard to understand.

Top answer
1 of 3
3
Ok, lets start with 'lastIndexOf'. Imagine this const array = ['a', 'a', 'b', 'b', 'c', 'c', 'c'] Now what they ask you to do is tell them where in that array the last 'b' is located. So you do array.lastIndexOf('b') Which will give you the number 3, since the first position is 0. Now you can work with that information. Lets say you want to clear all the old data. The new data is the fancy 'c' the client wants, everything else is to be ignored. Slice takes two arguments, start of what you want to extract and end, but end is optional, which means you can just use the start. So if you know that the 'c' starts after 'b', your start will be the position after 'b'. lastIndexOf tells you where 'b' is, so you can combine them like this: const newArray = array.slice(array.lastIndexOf('b') + 1) This will return only the three 'c's. If you had used slice(2, 4), it would start at the 3rd element, which is a 'b', include the 4th element, also a 'b' and end at the 5th element (index 4). So it would ignore the rest and the new array would be ['b', 'b', 'c']. I'm on the phone in the train, so this could be poorly formatted and maybe not well explained, please let me know if so, I'll try again.
2 of 3
2
You can find the docs for these methods here: String: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/slice https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/lastIndexOf Array: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/lastIndexOf
🌐
JavaScript Tutorial
javascripttutorial.net › home › javascript string methods › string.prototype.lastindexof()
JavaScript String lastIndexOf() Method
November 3, 2024 - In this tutorial, you'll learn how to use the JavaScript String lastIndexOf() method to locate the last occurrence of a substring in a string.
🌐
Vultr Docs
docs.vultr.com › javascript › standard-library › Array › lastIndexOf
JavaScript Array lastIndexOf() - Find Last Index of Element | Vultr Docs
November 29, 2024 - The lastIndexOf() method in JavaScript is a robust and versatile tool for locating the last index of an element in an array. With the ability to specify a starting index and differentiate between elements in varied orders, it effectively addresses ...
🌐
Scaler
scaler.com › home › topics › javascript string lastindexof()
JavaScript String lastIndexOf() - Scaler Topics
February 14, 2024 - The lastIndexOf method in javascript returns the index of the last occurrence of the specified substring. Given a second argument, a number, the lastIndexOf() method returns the last occurrence of the specified substring at an index less than ...
🌐
TutorialsPoint
tutorialspoint.com › javascript › string_lastindexof.htm
JavaScript String lastIndexOf() Method
The JavaScript String lastIndexOf() method is used to find the index of the last occurrence of the specified substring in the original string. It returns the position of the substring, or -1 if the substring is not present. For example,
Top answer
1 of 7
19

Personally, I wouldn't choose either solution. Here is why:

LastIndexOf:

The problem lies in the comparing of elements while searching through the array. It does compare the elements using strict equality. Therefore comparing objects will always fail, except they are the same. In OP case they are different.

Slice & reverse one-liner @adeneo

Given an array of three elements [{key: A},{key: B},{key: C}] and the lookup for the last index of key = D will give you an index of 3. This is wrong as the last index should be -1 (Not found)

Looping through the array

While this is not necessarily wrong, looping through the whole array to find the element isn't the most concise way to do it. It's efficient yes, but readability can suffer from it. If I had to choose one, I'd probably choose this one. If readability / simplicity is your friend, then below is yet one more solution.


A simple solution

We can make lastIndexOf work, we just need to make the value comparable (strict equality conform). Or simply put: we need to map the objects to a single property that we want to find the last index of using javascript's native implementation.

const arr = [ { key: "a" }, { key: "b" }, { key: "c" }, { key: "e" }, { key: "e" }, { key: "f" } ];

arr.map(el => el.key).lastIndexOf("e"); //4
arr.map(el => el.key).lastIndexOf("d"); //-1

// Better:
const arrKeys = arr.map(el => el.key);
arrKeys.lastIndexOf("c"); //2
arrKeys.lastIndexOf("b"); //1

A fast solution

Simple backwards lookup (as concise and as fast as possible). Note the -1 return instead of null/undefined.

const arr = [ { key: "a" }, { key: "b" }, { key: "c" }, { key: "e" }, { key: "e" }, { key: "f" } ];

const lastIndexOf = (array, key) => {
  for(let i = array.length - 1; i >= 0; i--){
    if(array[i].key === key)
      return i;
  }
  return -1;
};

lastIndexOf(arr, "e"); //4
lastIndexOf(arr, "x"); //-1
2 of 7
4

With ES2015 and findIndex you can pass a callback to look for an objects key.

If you make a copy of the array, and reverse it, you can find the last one by subtracting that index from the total length (and 1, as arrays are zero based)

It's not very efficient, but it's one line, and works well for normally sized arrays i.e. not a million indices

var idx = arr.length - 1 - arr.slice().reverse().findIndex( (o) => o.key == 'key' );

Show code snippet

var arr = [{key : 'not'}, {key : 'not'}, {key : 'key'}, {key : 'not'}];

var idx = arr.length - 1 - arr.slice().reverse().findIndex( (o) => o.key == 'key' ); // 2

console.log(idx)
Run code snippetEdit code snippet Hide Results Copy to answer Expand

A more efficient approach would be to iterate backwards until you find the object you're looking for, and break the loop

var arr = [{key: 'not'}, {key: 'not'}, {key: 'key'}, {key: 'not'}];

var idx = (function(key, i) {
  for (i; i--;) {
    if (Object.values(arr[i]).indexOf(key) !== -1) {
      return i;
      break;
    }
  }   return -1;
})('key', arr.length);

console.log(idx)
Run code snippetEdit code snippet Hide Results Copy to answer Expand

🌐
Microsoft Learn
learn.microsoft.com › en-us › dotnet › api › system.string.lastindexof
String.LastIndexOf Method (System) | Microsoft Learn
Reports the zero-based index position of the last occurrence of a specified Unicode character or string within this instance. The method returns -1 if the character or string is not found in this instance.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › TypedArray › lastIndexOf
TypedArray.prototype.lastIndexOf() - JavaScript | MDN
The lastIndexOf() method of TypedArray instances returns the last index at which a given element can be found in the typed array, or -1 if it is not present. The typed array is searched backwards, starting at fromIndex.
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › javascript-array-lastindexof-method
JavaScript Array lastIndexOf() Method - GeeksforGeeks
July 15, 2024 - The JavaScript Array lastIndexOf() Method is used to find the index of the last occurrence of the search element provided as the argument to the function.
🌐
TechOnTheNet
techonthenet.com › js › string_lastindexof.php
JavaScript: String lastIndexOf() method
This JavaScript tutorial explains how to use the string method called lastIndexOf() with syntax and examples. In JavaScript, lastIndexOf() is a string method that is used to find the location of a substring in a string, searching the string backwards.