--- title: String.prototype.lastIndexOf() slug: Web/JavaScript/Reference/Global_Objects/String/lastIndexOf tags: - Cadena - JavaScript - Prototipo - Referencia - metodo translation_of: Web/JavaScript/Reference/Global_Objects/String/lastIndexOf original_slug: Web/JavaScript/Referencia/Objetos_globales/String/lastIndexOf ---
{{JSRef("Objetos_globales", "String")}}
El método lastIndexOf() devuelve la posicion (indice) en la que se encuentra el valorBusqueda, dentro del objeto String que realiza la llamada, de la última ocurrencia del valor especificado; o -1 si no se halla. La búsqueda se realiza empezando por el final de la cadena que realiza la llamada, empezando en indiceDesde.
cadena.lastIndexOf(valorBusqueda[, indiceDesde])
valorBusquedaindiceDesdeLos caracteres de una cadena se indexan de izquierda a derecha. El índice del primer carácter es 0, y el índice del último carácter es nombreCadena.length - 1.
"canal".lastIndexOf("a") // returns 3
"canal".lastIndexOf("a",2) // returns 1
"canal".lastIndexOf("a",0) // returns -1
"canal".lastIndexOf("x") // returns -1
El método lastIndexOf es sensible a mayúsculas. Por ejemplo, la siguiente expresión devuelve -1:
"Ballena Azul, Ballena Asesina".lastIndexOf("azul")
indexOf y lastIndexOfThe following example uses indexOf and lastIndexOf to locate values in the string "Brave new world".
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"))