Array.prototype.every
Checks that every element of an array passes a test.
Available since: ES5
Syntax
array.every(callbackFn)Parameters
| Parameter | Type | Description |
|---|---|---|
| callbackFn | Function | A 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
falseAre all the form fields filled in:
const fields = { name: 'Anna', email: '', city: 'Berlin' };
console.log(Object.values(fields).every((value) => value !== ''));Output:
falseNotes
For an empty array every() always returns true: there is no element that breaks the rule.
See also
- Array.prototype.some — Checks whether at least one element passes a test.
- Array.prototype.filter — Creates a new array from the elements that pass a test.
Related recipes
In the tutorial
- Searching arrays — How to find an element in a JavaScript array: includes, indexOf, find, findIndex, some and every.