Javascript Array lastIndexOf Tutorial | JS Array lastIndexOf() Example

Like Tweet Pin it Share Share Email
Javascript Array lastIndexOf Tutorial | JS Array lastIndexOf() Example

Javascript Array lastIndexOf Tutorial | Array.prototype.lastIndexOf() Example is today’s topic. The lastIndexOf() method searches an array for the specified element and returns its position. The search will start backward at the specified position, or the end if no start position is specified, and end the search at the beginning of the array. It returns -1 if an element is not found. If you want to search from the start to end, use the indexOf() method.

Javascript Array lastIndexOf Tutorial

The lastIndexOf compares searchElement to elements of the Array using strict equality (the same method used by the ===, or triple-equals, operator).

The array lastIndexOf() syntax is following.

array.lastIndexOf(item, index)

The item parameter is required, and it is the item to search for.

The index parameter is optional, and it is index at which to start searching backward. Defaults to the array’s length, i.e., the whole array will be searched. If the index is greater than or equal to the length of the array, the whole array will be searched. If the negative argument is passed then, it is taken as the offset from the end of the array.

The lastIndexOf() function returns the index of the found element from the last.

See the following code example.

// app.js

let shows = ["GoT", "Chernobyl", "Breaking Bad", "Suits", "Friends"];
let output = shows.lastIndexOf("Suits");
console.log(output);

See the output.

Javascript Array lastIndexOf Tutorial

If an element to search for is present more than once, the lastIndexOf method returns the position of the last occurrence.

See the following example.

// app.js

let shows = ["GoT", "Chernobyl", "GoT", "Suits", "Friends", "GoT", "Friends"];
let output = shows.lastIndexOf("Friends", 6);
console.log(output);

The length of the show’s array is 6.

Now, in the lastIndexOf() method, we have passed the start index which is 6. So it will start searching backward from array index 6 and if the element is found then its last index will be in the output.

In the above code, we are searching Friends string from the 6th index and also the 6th item is Friends so, the output will be 6 of the above example.

JS Array lastIndexOf() Example

#Pass the negative index

Okay, now negative index count from backward means the last element’s index will be -1 and the second last element’s index will be -2 and so on.

See the following example.

// app.js

let shows = ["GoT", "Chernobyl", "GoT", "Suits", "Friends", "GoT", "Friends"];
let output = shows.lastIndexOf("GoT", -4);
console.log(output);

In the above code, we are searching from the end of the array. So, -4th element will be “Suits” and we are searching for GoT.

Now, the only 0th and 2nd position got the GoT and the last index of term GoT is 2 counting from left to right which is 2. So the output will be 2.

Pass the negative index

Different from the indexOf method, the lastIndexOf() method searches for the element backward, starting at index.

The above method is the JavaScript extension to the ECMA-262 standard; as such it may not be present on other implementations of the standard. If we want to make it work, we need to add the Polyfills.

Finally, Javascript Array lastIndexOf Tutorial | JS Array lastIndexOf() Example is over.

Comments (0)

Leave a Reply

Your email address will not be published. Required fields are marked *