JavaScript Array lastIndexOf() method

The lastIndexOf() method in JavaScript is used to search an array for a specified element, and returns the index of the last occurrence of the element in the array. If the element is not found in the array, the lastIndexOf() method returns -1.

Here is an example of how the lastIndexOf() method works:

const array1 = [1, 2, 3, 4, 5, 3];

const result = array1.lastIndexOf(3);

// the result will be: 5

In the code example above, we first declare an array called array1 that contains the numbers 1 through 5, and also contains the number 3 twice. We then use the lastIndexOf() method to search the array1 array for the number 3. The lastIndexOf() method takes a single argument, which is the element to search for in the array. In this case, we search for the number 3.

As a result of calling the lastIndexOf() method, the result variable will be set to 5, which is the index of the last occurrence of the number 3 in the array1 array. If the element we were searching for was not found in the array, the result variable would be set to -1.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Blog at

%d bloggers like this: