every() Passes Empty Arrays, Decide Explicitly
every() returns true for an empty array because no item can contradict the rule; gate empty input explicitly instead of inheriting the vacuous default.
Code
const gates = {
// every() on empty arrays is vacuously true: no item contradicts the rule
allAdults: (ages) => ages.every((age) => age >= 18),
// some() on empty arrays is always false: no item can prove the rule
hasMinor: (ages) => ages.some((age) => age < 18),
};
console.log(gates.allAdults([19, 21])); // true
console.log(gates.allAdults([])); // true <- empty input slips through
console.log(gates.hasMinor([19, 21])); // false
console.log(gates.hasMinor([])); // false
// Decide the empty case explicitly instead of inheriting the vacuous default
function approvedForCheckout(ages) {
if (ages.length === 0) return false; // no guests is not a valid booking
return ages.every((age) => age >= 18);
}
console.log(approvedForCheckout([19, 21])); // true
console.log(approvedForCheckout([])); // falseOutput
Captured from a sandboxed run of this snippet in the build pipeline.
true
true
false
false
true
falseHow it works
The short version: everything is the function approvedForCheckout, doing all the work.
What to watch for
- Zero-fetch snippet: every identifier resolves inside the language distribution.
- The guard
ages.length === 0breaks the flow off at once with an earlyreturnwhen its condition holds.
Using it
The signature you start reading from: default function approvedForCheckout(ages)
Key statements
return ages.every((age) => age >= 18);
Interactive Tools for This Topic
FAQ
- What has to be installed first?
- Nothing — the runtime itself provides everything the code touches. The file defines `approvedForCheckout` at load time.
Report a problem with this exampleOpens a pre-filled issue on GitHub.