Array.prototype.every()
every(callbackFn) Returns:
boolean · Added in vES5 · Updated March 13, 2026 · Array Methods array every iteration
The every() method tests whether all elements in an array pass the condition implemented by the provided function. It returns a boolean value — true if the callback returns truthy for every element, false if any element fails.
This method is useful for validating data, checking permissions, or ensuring all items meet a threshold.
Syntax
every(callbackFn)
every(callbackFn, thisArg)
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
callbackFn | function | — | Function to test each element. Receives element, index, and array arguments. |
thisArg | any | undefined | Value to use as this when executing callbackFn. |
Callback Function Arguments
| Argument | Type | Description |
|---|---|---|
element | any | Current element being processed. |
index | number | Index of the current element. |
array | Array | Array that every() was called on. |
Examples
Basic usage
const numbers = [10, 20, 30, 40];
const allPositive = numbers.every(num => num > 0);
console.log(allPositive);
// true
const allOver25 = numbers.every(num => num > 25);
console.log(allOver25);
// false
Validating object properties
const users = [
{ name: "Alice", age: 25 },
{ name: "Bob", age: 30 },
{ name: "Charlie", age: 35 }
];
const allAdults = users.every(user => user.age >= 18);
console.log(allAdults);
// true
Checking array contents
const strings = ["hello", "world", "javascript"];
const allContainH = strings.every(str => str.includes("h"));
console.log(allContainH);
// false — only "hello" contains "h"
const allStartWithLower = strings.every(str => str[0] === str[0].toLowerCase());
console.log(allStartWithLower);
// true
With index parameter
const pairs = [2, 4, 6, 8];
// Check if even-indexed elements are even
const valid = pairs.every((num, index) => {
if (index % 2 === 0) return num % 2 === 0;
return true;
});
console.log(valid);
// true
Empty arrays
const empty = [Global_Objects::eval];
const result = empty.every(x => x > 10);
console.log(result);
// true — vacuous truth: every() returns true for empty arrays
Common Patterns
Form validation
const formData = {
username: "john_doe",
email: "john@example.com",
password: "secret123"
};
const isValid = Object.values(formData).every(value => value.length > 0);
console.log(isValid);
// true
Range checking
const temperatures = [22, 25, 23, 24, 21];
const withinRange = temperatures.every(temp => temp >= 20 && temp <= 30);
console.log(withinRange);
// true
Permission checks
const userRoles = ["reader", "commenter"];
const requiredRole = "reader";
const hasAccess = userRoles.every(role => role === requiredRole || role === "admin");
// Returns true if user has at least reader or admin role
console.log(hasAccess);
// true
Performance Notes
every()stops iteration as soon as it finds a failing element- For large arrays, prefer
every()overfilter().length === array.length(which processes all elements) - The callback function should be pure for predictable results
See Also
- array::some — tests if any element passes
- array::filter — creates new array with passing elements
- array::find — returns first matching element