JavaScript Array.some() and every() - Test Array Conditions

Learn Array.some() and every() for testing conditions across arrays. Covers short-circuiting, empty arrays, and practical validation patterns. Free reference.

Testing

Detailed Explanation

Understanding some() and every()

These two methods test whether elements in an array satisfy a condition. Both short-circuit for performance — they stop iterating as soon as the result is determined.

Array.some()

Returns true if at least one element passes the test:

const nums = [1, 2, 3, 4, 5];
nums.some(n => n > 3);   // true (4 passes)
nums.some(n => n > 10);  // false (none pass)

Short-circuits: stops at the first truthy result.

Array.every()

Returns true if all elements pass the test:

const ages = [18, 21, 25, 30];
ages.every(a => a >= 18);  // true (all pass)

const mixed = [18, 15, 21];
mixed.every(a => a >= 18); // false (15 fails)

Short-circuits: stops at the first falsy result.

Empty Array Behavior

This often surprises developers:

[].some(x => true);   // false (vacuously false)
[].every(x => false);  // true  (vacuously true)

every() on an empty array returns true because the condition is trivially satisfied when there are no elements to test against. This follows mathematical logic (universal quantification).

Practical Patterns

Form validation:

const fields = [
  { name: "email", valid: true },
  { name: "password", valid: false }
];
const allValid = fields.every(f => f.valid);
// allValid: false — show form errors
const hasErrors = fields.some(f => !f.valid);
// hasErrors: true — at least one error

Feature detection:

const requiredAPIs = ["fetch", "Promise", "Symbol"];
const isSupported = requiredAPIs.every(api => api in window);

some() vs includes()

  • includes() checks for a specific value using strict equality
  • some() tests against a condition (callback function)
[1, 2, 3].includes(2);       // true
[1, 2, 3].some(n => n > 2);  // true (more flexible)

Use Case

Use some() for checking if any item in a list meets a condition (has errors, is selected, matches a search). Use every() for validating that all items meet requirements before proceeding (form validation, permission checks, batch operation guards).

Try It — JavaScript Array Methods Reference

Open full tool