String.lastIndexOf

lastIndexOf — Finds the position of the last occurrence of a string within a string.

Description

String.lastIndexOf(string string, [integer offset])

Parameters

Name Description Type Default Optional
string string No
offset Counting from beginning of string. integer Yes

Return values

number primitive.

Examples

Example #1 – lastIndexOf example
var txt = 'Javascript is awesome!'; console.log(txt.lastIndexOf('a')); // Will return 14. console.log(txt.lastIndexOf('b')); // Will return -1.

When passing the secions parameter, it will start searching at the given position (counting from the beginning). This example illustrates this with a string that is cut to the same length.

var txt = 'Javascript is awesome!'; console.log(txt.lastIndexOf('a', 9)); // Will return 3. console.log('Javascript'.lastIndexOf('a')); // Will return 3.

External references