Searching Arrays with indexOf and lastIndexOf

In JavaScript, the indexOf() method returns the first index at which a specified element can be found in an array. If it does not exist in the array, -1 is returned instead. Similarly, lastIndexOf() returns the last index where an element appears or -1 if it doesn't exist within the array. These methods are incredibly useful when searching for elements and are commonly used in various coding instances.

Description

The lastIndexOf() method compares searchElement to elements of the array using strict equality (the same algorithm used by the === operator). NaN values are never compared as equal, so lastIndexOf() always returns -1 when searchElement is NaN.

The lastIndexOf() method skips empty slots in sparse arrays.

The lastIndexOf() method is generic. It only expects the this value to have a length property and integer-keyed properties.