JS Built-ins
← Array74 / 226
ArrayArray.prototype.findIndexbeginner

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

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

findIndex() returns -1 when no element satisfies the predicate β€” always check for this.

Array.prototype.findIndex() β€” no match

When no element satisfies the predicate, findIndex() returns -1. This is consistent with indexOf() and lastIndexOf().

Challenge: Implement findGtHundred(nums) that returns the index of the first number greater than 100, or -1 if none.

findGtHundred([10, 200, 50]) // β†’ 1
findGtHundred([1, 2, 3])     // β†’ -1
πŸ’‘ Hints (2)
  • β€’ Checking the result with `=== -1` tells you whether a match was found.
  • β€’ `> 100` excludes exactly 100; use `>= 100` to include it.