JS Built-ins
← Array172 / 226
ArrayArray.prototype.somebeginner

Array.prototype.some() β€” check for negatives

πŸ“– Usage Example
const nums = [1, 3, 5, 8]
nums.some(n => n % 2 === 0)  // β†’ true  (8 is even)

Use some() to check whether at least one element satisfies a condition.

Array.prototype.some()

Array.prototype.some(predicate) returns true if at least one element satisfies the predicate, false otherwise. It short-circuits as soon as a match is found.

Challenge: Implement hasNegative(nums) that returns true if any number in the array is less than 0.

hasNegative([1, -2, 3])  // β†’ true
hasNegative([1, 2, 3])   // β†’ false
πŸ’‘ Hints (2)
  • β€’ `some(n => n < 0)` checks every element until it finds one less than zero.
  • β€’ `some()` on an empty array always returns `false` β€” there are no elements to match.