Skip to content

WebDevHubs

  • Home
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • Java
  • Selenium
  • PHP
  • Python
  • Programs
  • Toggle search form

JavaScript Array lastIndexOf() Method

Posted on June 14, 2025June 14, 2025 By Admin No Comments on JavaScript Array lastIndexOf() Method

JavaScript Array lastIndexOf() method is used to search for the last occurrence of a specified element in an array. It returns the index of the element if found, or -1 if the element does not exist in the array. The search is performed in reverse order, starting from the end of the array.

JavaScript Array lastIndexOf() Method

Pre-requisites to Learn

  • JavaScript Array

Syntax

arr.lastIndexOf(searchElement, fromIndex);

Parameters

ParameterDescription
searchElement (Required)The element to search for in the array.
fromIndex (Optional)The index to start searching backward from. Defaults to the last index of the array.

Return Value

The lastIndexOf() method returns the index of the last occurrence of the searchElement in the array.-1 if the searchElement is not found.

Note:

  • Searches Backward: Looks for the element starting from the end of the array.
  • Handles Duplicates: Returns the last index of the specified element, even if it appears multiple times.
  • Uses Strict Equality (===): Compares elements using the strict equality operator, which means 5 is not equal to '5'.

Examples of JavaScript Array lastIndexOf() Method

Example 1: Finding the last occurrence of 2, which is at index 3.

let arr = [1, 2, 3, 2, 4];

let lastIndex = arr.lastIndexOf(2);
console.log(lastIndex); // Output: 3

Example 2: Start searching backward from index 2 and finds the first occurrence of 2 at index 1.

let arr = [1, 2, 3, 2, 4];

let lastIndex = arr.lastIndexOf(2, 2);
console.log(lastIndex); // Output: 1

Example 3: Element Not Found – If the element is not found, the method returns -1.

let numbers = [1, 2, 3, 4];

let lastIndex = numbers.lastIndexOf(5);
console.log(lastIndex); // Output: -1

Example 4: Identifies the last occurrence of 'banana' at index 3.

let arr = ['apple', 'banana', 'cherry', 'banana'];

let lastIndex = arr.lastIndexOf('banana');
console.log(lastIndex); // Output: 3

Example 5: A negative fromIndex counts backward from the end of the array. In this case, -3 corresponds to index 2.

let arr = [10, 20, 30, 40, 50];

let lastIndex = arr.lastIndexOf(30, -3);
console.log(lastIndex); // Output: 2

Example 6: The lastIndexOf() method is case-sensitive, so 'apple' and 'Apple' are treated as different elements.

let arr = ['Apple', 'Banana', 'apple'];

let lastIndex = arr.lastIndexOf('apple');
console.log(lastIndex); // Output: 2

let caseMismatch = arr.lastIndexOf('Apple');
console.log(caseMismatch); // Output: 0

Example 7: The lastIndexOf() method treats empty slots in sparse arrays as undefined.

let sparseArray = [1, , 3, , 5];

let lastIndex = sparseArray.lastIndexOf(undefined);
console.log(lastIndex); // Output: 3

Supported Browsers

BrowserSupport
Chrome1+
Firefox1+
Safari3+
Edge12+
Opera4+
Internet Explorer9+

Comparison with Other Methods

MethodPurpose
Array.lastIndexOf() MethodSearches for the last occurrence of an element, starting from the end of the array.
Array.indexOf() MethodSearches for the first occurrence of an element, starting from the beginning of the array.
Array.includes() MethodChecks if an array contains a value, but does not return the index.

JavaScript, Web Technologies Tags:JavaScript-Array, JavaScript-Array-Method, JavaScript-Method

Post navigation

Previous Post: JavaScript Array map() Method
Next Post: JavaScript Array keys() Method

Leave a Reply Cancel reply

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

  • JavaScript Array Methods
  • JavaScript Array.from() Method
  • JavaScript Array isArray() Method
  • JavaScript Array at() Method
  • JavaScript Array every() Method
  • JavaScript Array filter() Method
  • JavaScript Array find() Method
  • JavaScript Array forEach() Method
  • JavaScript Array includes() Method
  • JavaScript Array indexOf() Method
  • JavaScript Array join() Method
  • JavaScript Array keys() Method
  • JavaScript Array lastIndexOf() Method
  • JavaScript Array map() Method
  • JavaScript Array pop() Method
  • JavaScript Array push() Method
  • JavaScript Array reduce() Method
  • JavaScript Array reduceRight() Method
  • JavaScript Array reverse() Method
  • JavaScript Array shift() Method
  • JavaScript Array slice() Method
  • JavaScript Array some() Method
  • JavaScript Array sort() Method
  • JavaScript Array splice() Method
  • JavaScript Array toLocaleString() Method
  • JavaScript Array toReversed() Method
  • JavaScript Array toSorted() Method
  • JavaScript Array toSpliced() Method
  • JavaScript Array toString() Method
  • JavaScript Array unshift() Method
  • JavaScript Array values() Method
  • JavaScript Array with() Method

Recent Posts

  • Java ArrayList trimToSize() Method
  • Java ArrayList toArray() Method
  • Java ArrayList subList() Method
  • Java ArrayList spliterator() Method
  • Java ArrayList sort() Method

Recent Comments

No comments to show.

Important Pages

  • About Us
  • Contact Us
  • Terms of Use
  • Privacy Policy

Web Development

  • HTML
  • CSS
  • JavaScript
  • PHP

Programming Languages

  • Java
  • Python
  • PHP
  • Programs

Others

  • Selenium
  • Lodash
  • Java ArrayList
  • JavaScript Array Methods

Copyright © 2025 WebDevHubs.

Powered by PressBook Green WordPress theme