Upcoming Course: Code Your Own Business w/ React + GraphQL!
We're live-coding on Twitch! Join us!

Basics

The lastIndexOf() method returns the last position within a String object which an element can be found. The search starts from the last position of the String object. The method will return -1 if the specified parameter cannot be found.

"hello world scotch world".lastIndexOf("world")
// 19

Syntax

const indexOfLastOccurence = string.lastIndexOf(searchParam, startIndex)

2 Parameters

searchParam This is the string value to search for within the String object - required

startIndex This is the index at which the search will begin. Search typically begins from the string.length - 1 index, which is the last position of the object. If the length of the provided string is 0, or if no item was found, it returns -1.

Returns a number

The method returns a number whch is the index of the last occurence of the search parameter.

This method is case sensitive

Common Uses and Snippets

Find the last word repitition in a list

With the lastIndexOf() method, the index of the last occurrence of a particular string can be obtained.

Like this article? Follow @iChuloo on Twitter