The some() and every() Methods in JavaScript
When working with arrays in JavaScript, you often need to check whether all elements meet certain conditions, or if at least one element satisfies them.
In these cases, your best friends are the some() and every() methods.
What does some() do?
- Returns true if at least one element meets the condition
- Otherwise returns false
Syntax
array.some(callback(element, index, array))
Example
const ages = [12, 17, 19, 25];
const hasAdult = ages.some(age => age >= 18);
console.log(hasAdult); // true
In this example, since 19 and 25 are greater than or equal to 18, some() returns true.
What about every()?
- Returns true if all elements meet the condition
- Returns false if even one element fails
Syntax
array.every(callback(element, index, array))
Example
const ages = [19, 22, 25];
const allAdults = ages.every(age => age >= 18);
console.log(allAdults); // true
If even one element was 17, the result would become false.
some() vs every() Comparison
| Purpose | some() | every() |
| At least one meets condition | ✅ true if any satisfies | ✅ true only if all satisfy |
| Limitation | Stops at first satisfying element | Stops at first non-satisfying element |
Practical Examples
Check if at least one user is online
const users = [
{ name: "Aram", online: false },
{ name: "Mariam", online: true },
{ name: "Sona", online: false }
];
const someoneOnline = users.some(user => user.online);
console.log(someoneOnline); // true
Check if all products are in stock
const products = [
{ name: "Laptop", inStock: true },
{ name: "Mouse", inStock: true },
{ name: "Keyboard", inStock: false }
];
const allAvailable = products.every(product => product.inStock);
console.log(allAvailable); // false