JS Built-ins
ControlFlowbeginner

Control Flow β€” early return pattern

Early return for guard clauses

Returning early from a function when a condition is met avoids deep nesting and makes code easier to read.

Challenge: Write findFirst(arr, target) that:

  • Loops through arr
  • Returns the index of the first element equal to target
  • Returns -1 if not found
πŸ’‘ Hints (1)
  • β€’ Return inside the loop immediately when you find a match