JS: lastIndexOf() Method

In previous example, we saw how JavaScript treat string as an array and allow us to access its character using indexOf() method.


But sometimes, there are more than one occurrence of a character in the string. To know the index of last occurrence of a string. JavaScript has <span style="color:#a30500" class="tadv-color">lastIndexOf()</span> method in the string class.


Here is an example of the method – lastIndexOf().

<!DOCTYPE html>
   <title>JS: lastIndexOf() Method</title>
   <meta charset="utf-8">
   var animal = "elephant";
var out = animal.lastIndexOf('e');
//outputs = 2

In the above example, there are two occurrence of character ‘e’. The <span style="color:#a30000" class="tadv-color">lastIndexOf()</span> method returns the index value of last occurrence of ‘e’ which is 2.


Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.