javascript – indexOf starting on the right

Question:

Problem

I would like that when I did "asdhusaidhi asdhasuidhu".indexOf('s') it would return 17 referring to the last s of the string, for example, it would start looking for the right..

but instead it returned 1 for the first s found

Question?

How can I get him to start looking for the right?

Answer:

You can use .lastIndexOf() which returns the last index where this string 's' was found.

"asdhusaidhi asdhasuidhu".lastIndexOf('s'); // 17

jsFiddle: https://jsfiddle.net/d0jbb6x7/

Scroll to Top