← All languages
JavaScript function of the day
Random

Array.prototype.every

Test whether all elements pass the provided function.

Description

The every() method tests whether all elements in the array pass the test implemented by the provided function. It returns true if the callback returns a truthy value for every element; otherwise, it returns false.

The method short-circuits: it stops iterating as soon as the callback returns a falsy value for an element. This makes it efficient when a failing element appears early.

every() returns true for an empty array regardless of the callback (vacuous truth). It is commonly used for validation, such as checking that all items in a list meet a minimum requirement.

Arguments

NameDescriptionOptional
callbackFn A function to test each element, returning true or false. No
thisArg Value to use as this when executing callbackFn. Yes

Example

const nums = [2, 4, 6];
nums.every(x => x % 2 === 0);  // true
nums.every(x => x > 3);        // false

Reference