Array.prototype.everyLoading saved progress…

Array.prototype.every

Array.prototype.every checks whether all elements of an array pass a test. It returns false the instant it finds one that fails, and true only if every element passes. It's the exact mirror of some: some looks for one success, every looks for one failure.

Implement arrayEvery(arr, callback, thisArg). Call callback on each element with (value, index, array); the moment one returns a falsy value, return false and stop. If none fails, return true. An empty array passes vacuously — with no element to break the rule, every returns true.

Signature

function arrayEvery(arr, callback, thisArg) {
  // returns true if callback(value, index, arr) is truthy for EVERY element,
  // false otherwise; stops at the first falsy result.
}

Examples

arrayEvery([2, 4, 6], (n) => n % 2 === 0); // true  — all even
arrayEvery([2, 3, 4], (n) => n % 2 === 0); // false — 3 fails
arrayEvery([], () => false); // true — nothing to break the rule

Notes

  • Short-circuit — stop as soon as the callback returns falsy. Don't scan the rest.
  • Empty array — returns true (vacuous truth). This surprises people; it's the opposite of some's empty-array false.
  • Falsiness — any falsy return (0, '', null, undefined, NaN) counts as a failure, not just literal false.
  • Sparse holes — skip holes; don't test them.
  • thisArg — when provided, sets this inside a regular callback.
Loading editor…