xxxxxxxxxx
'canal'.lastIndexOf('a'); // retorna 3
'canal'.lastIndexOf('a', 2); // retorna 1
'canal'.lastIndexOf('a', 0); // retorna -1
'canal'.lastIndexOf('x'); // retorna -1
'canal'.lastIndexOf('c', -5); // retorna 0
'canal'.lastIndexOf('c', 0); // retorna 0
'canal'.lastIndexOf(''); // retorna 5
'canal'.lastIndexOf('', 2); // retorna 2
xxxxxxxxxx
//The lastIndexOf() method returns the index of the last occurrence of a specified text in a string:
let str = "Please locate where 'locate' occurs!";
str.lastIndexOf("locate");
// >> 21
//if you find this answer is useful ,
//upvote ⇑⇑ , so can the others benefit also . @mohammad alshraideh ( ͡~ ͜ʖ ͡°)
xxxxxxxxxx
// lastIndexOf only requires a single value
// and returns the index of last instance of value found in array
const cities = [
"Orlando",
"Denver",
"Edinburgh",
"Chennai",
"Denver",
"Eskisehir",
"Yokohama",
"Denver",
"Chennai",
];
console.log(cities.lastIndexOf("Denver"));
// Expected output is 7
xxxxxxxxxx
const fruits = ["Apple", "Orange", "Apple", "Mango"];
let position = fruits.lastIndexOf("Apple") + 1;
xxxxxxxxxx
var numbers = [2, 5, 9, 2];
numbers.lastIndexOf(2); // 3
numbers.lastIndexOf(7); // -1
numbers.lastIndexOf(2, 3); // 3
numbers.lastIndexOf(2, 2); // 0
numbers.lastIndexOf(2, -2); // 0
numbers.lastIndexOf(2, -1); // 3
xxxxxxxxxx
var anyString="Brave new world"
// Displays 8
document.write("<P>The index of the first w from the beginning is " +
anyString.indexOf("w"))
// Displays 10
document.write("<P>The index of the first w from the end is " +
anyString.lastIndexOf("w"))
// Displays 6
document.write("<P>The index of 'new' from the beginning is " +
anyString.indexOf("new"))
// Displays 6
document.write("<P>The index of 'new' from the end is " +
anyString.lastIndexOf("new"))
xxxxxxxxxx
let str = "Please locate where 'locate' occurs!";
str.lastIndexOf("locate");