EN
JavaScript - String lastIndexOf() method example
0 points
xxxxxxxxxx
1
const text = 'ABC ABC';
2
3
console.log(text.lastIndexOf('A')); // 4
4
console.log(text.lastIndexOf('B')); // 5
5
console.log(text.lastIndexOf('C')); // 6
6
7
console.log(text.lastIndexOf('X')); // -1
8
9
console.log(text.lastIndexOf('ABC')); // 4
10
console.log(text.lastIndexOf('XYZ')); // -1
11
12
console.log(text.lastIndexOf('B', 2)); // 1
13
console.log(text.lastIndexOf('B', 6)); // 5
Syntax |
|
Parameters |
|
Result | The method returns the index of the last occurrence of searchString or -1 if not found. |
Description |
The If the second argument is given (a number), the method returns the last occurrence of the specified substring at an index less than or equal to the specified number. |