some and every
So far, you have learned array methods that return different kinds of results.
map transforms every item and returns a new array.
filter keeps matching items and returns a new array.
find returns the first matching item.
reduce builds one final value.
But sometimes you do not need the items themselves.
Sometimes you only need to answer a yes/no question:
Does at least one item match?
Do all items match?That is what .some() and .every() are for.
What Do some and every Return?
Both methods return a boolean:
truefalse
They do not return a new array.
They do not return the matching item.
They answer a question about the array.
const numbers = [1, 2, 3, 4, 5];
const hasEven = numbers.some((number) => number % 2 === 0);
const allEven = numbers.every((number) => number % 2 === 0);
console.log(hasEven); // true
console.log(allEven); // falseThe Big Difference
Use .some() for "at least one."
Use .every() for "all."
| Method | Question | Returns true when |
|---|---|---|
some |
Does at least one item pass? | One or more items pass |
every |
Do all items pass? | Every item passes |
You can think of them like logical operators for arrays:
.some()is like||across many items.every()is like&&across many items
The some Method
The .some() method checks whether at least one element passes a condition.
const numbers = [1, 2, 3, 4, 5];
const hasEven = numbers.some((number) => {
return number % 2 === 0;
});
console.log(hasEven); // trueThe condition is:
number % 2 === 0This checks whether a number is even.
Since 2 and 4 are even, .some() returns true.
some Step by Step
const numbers = [1, 3, 5, 8, 9];
const hasEven = numbers.some((number) => {
return number % 2 === 0;
});
console.log(hasEven); // trueStep by step:
| Number | Condition result | What happens? |
|---|---|---|
1 |
false |
Keep checking |
3 |
false |
Keep checking |
5 |
false |
Keep checking |
8 |
true |
Return true and stop |
.some() stops as soon as it finds one match.
It does not need to check 9.
The every Method
The .every() method checks whether all elements pass a condition.
const scores = [85, 92, 78, 90];
const allPassed = scores.every((score) => {
return score >= 70;
});
console.log(allPassed); // trueEvery score is at least 70, so .every() returns true.
If one item fails, the result is false.
const scores = [85, 92, 65, 90];
const allPassed = scores.every((score) => score >= 70);
console.log(allPassed); // falseThe score 65 fails the condition.
every Step by Step
const numbers = [2, 4, 6, 7, 8];
const allEven = numbers.every((number) => {
return number % 2 === 0;
});
console.log(allEven); // falseStep by step:
| Number | Condition result | What happens? |
|---|---|---|
2 |
true |
Keep checking |
4 |
true |
Keep checking |
6 |
true |
Keep checking |
7 |
false |
Return false and stop |
.every() stops as soon as it finds one failure.
It does not need to check 8.
Basic Syntax
Both methods use the same callback style as other array methods.
array.some(function (currentValue, index, array) {
return condition;
});array.every(function (currentValue, index, array) {
return condition;
});The callback can receive three arguments:
| Argument | Meaning | Common? |
|---|---|---|
currentValue |
The current element being checked | Yes |
index |
The index of the current element | Sometimes |
array |
The original array | Rarely |
Most of the time, you only need the first argument.
Permission Checks With some
Use .some() when one matching item is enough.
const users = [
{ name: "Alice", role: "user" },
{ name: "Bob", role: "admin" },
{ name: "Charlie", role: "user" },
];
const hasAdmin = users.some((user) => {
return user.role === "admin";
});
console.log(hasAdmin); // trueThis answers the question:
Is at least one user an admin?Since Bob is an admin, the answer is true.
Validation With every
Use .every() when all items must pass.
const formInputs = [
{ field: "username", isValid: true },
{ field: "email", isValid: true },
{ field: "password", isValid: false },
];
const isFormValid = formInputs.every((input) => {
return input.isValid;
});
console.log(isFormValid); // falseThis answers the question:
Are all form inputs valid?One input is invalid, so the answer is false.
Checking for Errors
.some() is useful for checking whether any item has a problem.
const fields = [
{ name: "username", error: null },
{ name: "email", error: "Invalid email" },
{ name: "password", error: null },
];
const hasErrors = fields.some((field) => {
return field.error != null;
});
console.log(hasErrors); // trueThe field.error != null check matches both null and undefined.
If any field has an error, hasErrors is true.
Checking Data Completeness
.every() is useful when every item must have a required value.
const users = [
{ name: "Alice", email: "alice@example.com" },
{ name: "Bob", email: "bob@example.com" },
{ name: "Charlie", email: "charlie@example.com" },
];
const allHaveEmails = users.every((user) => {
return user.email;
});
console.log(allHaveEmails); // trueThis works because non-empty strings are truthy.
If an email could be an empty string, this condition would fail for that user.
Short-Circuiting
Both .some() and .every() short-circuit.
That means they stop early when the final answer is already known.
.some() stops when it finds the first passing item.
const numbers = [1, 3, 4, 6, 8];
const hasEven = numbers.some((number) => {
console.log(`Checking ${number}`);
return number % 2 === 0;
});
console.log(hasEven); // trueOutput:
Checking 1
Checking 3
Checking 4
trueIt does not check 6 or 8.
.every() stops when it finds the first failing item.
const numbers = [2, 4, 5, 6, 8];
const allEven = numbers.every((number) => {
console.log(`Checking ${number}`);
return number % 2 === 0;
});
console.log(allEven); // falseOutput:
Checking 2
Checking 4
Checking 5
falseIt does not check 6 or 8.
Empty Array Behavior
Empty arrays have special behavior.
.some() on an empty array returns false.
const items = [];
console.log(items.some((item) => item.isActive)); // falseThis makes sense:
There is not at least one matching item..every() on an empty array returns true.
const items = [];
console.log(items.every((item) => item.isActive)); // trueThis can feel strange at first.
The idea is:
No item failed the condition.This is called vacuous truth in logic.
In practical code, be careful when an empty array should not count as "valid."
Guarding Against Empty Arrays
If you need at least one item and all items must pass, combine .length with .every().
const fields = [];
const isValidForm = fields.length > 0 && fields.every((field) => field.isValid);
console.log(isValidForm); // falseThis means:
There must be at least one field,
and every field must be valid.This pattern is useful for validation.
some vs find
Use .some() when you only need a boolean.
Use .find() when you need the matching item.
const users = [
{ id: 1, name: "Alice" },
{ id: 2, name: "Bob" },
];
const hasUserTwo = users.some((user) => user.id === 2);
const userTwo = users.find((user) => user.id === 2);
console.log(hasUserTwo); // true
console.log(userTwo); // { id: 2, name: "Bob" }| Method | Returns |
|---|---|
some |
true or false |
find |
Matching item or undefined |
every vs filter
Use .every() when you only need to know whether all items pass.
Use .filter() when you need the matching items.
const scores = [80, 90, 100];
const allPassed = scores.every((score) => score >= 70);
const passingScores = scores.filter((score) => score >= 70);
console.log(allPassed); // true
console.log(passingScores); // [80, 90, 100]every returns a boolean.
filter returns an array.
Truthy and Falsy Return Values
The callbacks do not have to return literal true or false.
They can return truthy or falsy values.
const users = [
{ name: "Alice", email: "alice@example.com" },
{ name: "Bob", email: "" },
];
const everyoneHasEmail = users.every((user) => user.email);
console.log(everyoneHasEmail); // falseThe first email is a non-empty string, so it is truthy.
The second email is an empty string, so it is falsy.
For clarity, you can also write explicit boolean checks:
const everyoneHasEmail = users.every((user) => user.email !== "");Using Index With some and every
The second callback argument is the index.
const steps = ["start", "load", "finish"];
const hasFinishAfterStart = steps.some((step, index) => {
return step === "finish" && index > 0;
});
console.log(hasFinishAfterStart); // trueYou will not need the index every time.
But it is available when the condition depends on position.
Best Practices
Use .some() for "at least one" checks:
const hasAdmin = users.some((user) => user.role === "admin");Use .every() for "all items" checks:
const allPaid = invoices.every((invoice) => invoice.status === "paid");Use clear boolean conditions:
const hasExpensiveProduct = products.some((product) => product.price > 1000);Be careful with empty arrays and .every():
const isValid = fields.length > 0 && fields.every((field) => field.isValid);Use .find() if you need the actual item:
const admin = users.find((user) => user.role === "admin");Use .filter() if you need all matching items:
const admins = users.filter((user) => user.role === "admin");Common Mistakes
Mistake 1: Expecting some to Return the Matching Item
const users = [{ id: 1 }, { id: 2 }];
const result = users.some((user) => user.id === 2);
console.log(result); // truesome returns a boolean.
Use find if you need the item:
const user = users.find((user) => user.id === 2);Mistake 2: Expecting every to Return Matching Items
const scores = [80, 90, 100];
const result = scores.every((score) => score >= 70);
console.log(result); // trueevery returns a boolean.
Use filter if you need the items:
const passingScores = scores.filter((score) => score >= 70);Mistake 3: Forgetting return
const scores = [80, 90, 100];
const allPassed = scores.every((score) => {
score >= 70;
});
console.log(allPassed); // falseWith braces, arrow functions need an explicit return.
Correct:
const allPassed = scores.every((score) => {
return score >= 70;
});Or:
const allPassed = scores.every((score) => score >= 70);Mistake 4: Forgetting the Empty Array Edge Case
const fields = [];
const isValid = fields.every((field) => field.isValid);
console.log(isValid); // trueIf an empty form should not be valid, include a length check:
const isValid = fields.length > 0 && fields.every((field) => field.isValid);Quick Check
What does this return?
const numbers = [1, 3, 5, 8];
const result = numbers.some((number) => number % 2 === 0);It returns:
trueAt least one number is even.
What does this return?
const numbers = [2, 4, 6, 7];
const result = numbers.every((number) => number % 2 === 0);It returns:
falseThe number 7 is not even.
What does this return?
const items = [];
const result = items.every((item) => item.isActive);It returns:
trueNo item fails the condition, so .every() returns true for an empty array.
Summary
some and every answer yes/no questions about arrays.
somereturnstrueif at least one item passes the condition.everyreturnstrueonly if all items pass the condition.- Both methods return booleans, not arrays or elements.
- Both methods short-circuit.
somestops when it finds the first passing item.everystops when it finds the first failing item.someon an empty array returnsfalse.everyon an empty array returnstrue.- Use
findwhen you need the matching item. - Use
filterwhen you need all matching items.