JS Built-ins
← Array123 / 226
ArrayArray.prototype.lastIndexOfbeginner

Array.prototype.lastIndexOf() β€” returns -1 when not found

πŸ“– Usage Example
const arr = [1, 2, 3]
arr.lastIndexOf(9)  // β†’ -1  (not found)

lastIndexOf() returns -1 when the value is not present anywhere in the array.

Array.prototype.lastIndexOf() β€” not found

When the search value is not present in the array, lastIndexOf returns -1. This mirrors the behavior of indexOf.

Challenge: Implement notFound(arr, val) that returns the result of arr.lastIndexOf(val) (which will be -1 when the value is absent).

notFound([1, 2, 3], 99) // β†’ -1
notFound([], 0)         // β†’ -1
πŸ’‘ Hints (2)
  • β€’ `lastIndexOf` returns `-1` when no match is found β€” the same as `indexOf`.
  • β€’ You can use `result !== -1` to check whether the value was found.