every()
every<
T>(collection,predicate):boolean
every<
T>(collection,predicate):boolean
Checks if predicate returns truthy for all elements of collection.
Use array.every() or Object.values().every() directly instead.
Type Parameters
T: T
The type of elements in the array, or the object type.
Parameters
Overload 1:
collection: T[]
The collection to iterate over.
predicate: (value, index, array) => boolean
The function invoked per iteration.
Overload 2:
collection: T
The collection to iterate over.
predicate: (value, key, object) => boolean
The function invoked per iteration.
Returns: boolean
true if all elements pass the predicate check, else false.
See Also
Since
2.0.0
Also known as
all (Ramda) · every (Lodash, es-toolkit, Effect) · ❌ (Remeda, Radashi, Modern Dash, Antfu)
Example
const numbers = [1, 2, 3, 4, 5];
// ❌ Deprecated approach
const allPositive = every(numbers, n => n > 0);
console.log(allPositive); // true
// ✅ Recommended approach (for arrays)
const allPositiveNative = numbers.every(n => n > 0);
console.log(allPositiveNative); // true
// ✅ Recommended approach (for objects)
const obj = { a: 1, b: 2, c: 3 };
const allPositiveObj = Object.values(obj).every(n => n > 0);
console.log(allPositiveObj); // true
How it works?
Checks if all elements pass the predicate.
Deprecated: Use array.every() directly.
Native Equivalent
// ❌ every(arr, fn)
// ✅ arr.every(fn)
Use Cases
Validate all items match criteria 📌
Check if all elements satisfy a condition.
const ages = [18, 25, 30, 22];
ages.every(age => age >= 18);
// => true
Check form validity
Verify all form fields are valid.
const fields = [{ value: "a" }, { value: "b" }, { value: "" }];
fields.every(f => f.value.length > 0);
// => false
Ensure permissions granted
Check if all required permissions are granted.
const permissions = ["read", "write", "execute"];
const granted = ["read", "write", "execute"];
permissions.every(p => granted.includes(p));
// => true
Validate uploaded files meet constraints
Check that all files in an upload batch respect size and type limits. Essential for file upload forms, document management, and media libraries.
const maxSize = 5 * 1024 * 1024; // 5MB
const allowedTypes = ["image/png", "image/jpeg", "application/pdf"];
const files = [
{ name: "photo.jpg", size: 2_000_000, type: "image/jpeg" },
{ name: "doc.pdf", size: 1_500_000, type: "application/pdf" },
{ name: "huge.png", size: 8_000_000, type: "image/png" },
];
const allValid = every(files, (f) => f.size <= maxSize && allowedTypes.includes(f.type));
// => false (huge.png exceeds 5MB)