Encodera

Thanks for reading!

Donate to the site — we work hard for you.

JavaScript reference

Array.prototype.every

Checks that every element of an array passes a test.

Available since: ES5

Syntax

array.every(callbackFn)

Parameters

ParameterTypeDescription
callbackFnFunctionA test function: receives the element, its index and the array, and returns true or false.

Return value

booleantrue if every element passes the test.

Examples

Are all the numbers positive:

console.log([3, 8, 1].every((n) => n > 0));
console.log([3, -8, 1].every((n) => n > 0));

Output:

true
false

Are all the form fields filled in:

const fields = { name: 'Anna', email: '', city: 'Berlin' };
 
console.log(Object.values(fields).every((value) => value !== ''));

Output:

false

Notes

For an empty array every() always returns true: there is no element that breaks the rule.

See also

Related recipes

In the tutorial

  • Searching arrays — How to find an element in a JavaScript array: includes, indexOf, find, findIndex, some and every.

Thanks for reading!

Donate to the site — we work hard for you.