JavaScript some() and every(): Quantify Conditions
Quick answer
some() asks whether at least one present element qualifies; every() asks whether all present elements qualify. Both return Booleans.
How it works
Some stops on the first truthy callback result; every stops on the first falsy result. Both skip ordinary holes. Empty arrays give false and true respectively, because no witness or counterexample exists. Callbacks receive value, index, source.
const scores = [3, 8, 9];console.log(scores.some(n => n >= 8), scores.every(n => n >= 8));Expected output in Node.js:
true falseSome finds 8; every encounters 3.
console.log([].some(n => n > 0), [].every(n => n > 0));Expected output in Node.js:
false trueThere is no witness and no counterexample.
Common mistakes
Every on an empty list does not prove that any data was supplied. Check nonemptiness separately if required.
Practice
Check whether any item is out of stock and whether all quantities are nonnegative.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
JavaScript some() and every(): Quantify Conditions — Knowledge Check
Apply the taught concept and predict the output.
When does some stop traversing?
What must be added if every passing score is required and at least one score must exist?
Which result type is returned by every?
What is the exact console output in JavaScript some() and every(): Quantify Conditions example 1?
const scores = [3, 8, 9];console.log(scores.some(n => n >= 8), scores.every(n => n >= 8));What is the exact console output in JavaScript some() and every(): Quantify Conditions example 2?
console.log([].some(n => n > 0), [].every(n => n > 0));