JavaScript some() vs every(): Check Array Conditions

Learn the difference between JavaScript some() and every(), with practical examples for roles, form validation, and empty arrays.

JavaScript some() vs every(): Check Array Conditions

some() vs every() in JavaScript

When you need to check an array against a condition, two useful methods are some() and every(). Their names describe the difference:

  • some() asks: Does at least one item pass this test?
  • every() asks: Do all items pass this test?

Both methods return a Boolean (true or false). They do not change the original array.

Check whether any user role is admin

Imagine a user can have more than one role. Use some() to check whether at least one is "admin":

const userRoles = ["editor", "admin", "member"];

const hasAdminRole = userRoles.some((role) => role === "admin");

console.log(hasAdminRole); // true

some() calls the callback with each item until one returns a truthy result. Once it finds "admin", it returns true and stops checking the rest. If no role matches, it returns false.

This can be useful for an interface decision, such as whether to show an admin link. It does not replace server-side authorization: the server must still verify permission before allowing protected actions.

Check that every required field is filled

Use every() when each item must pass. For example, a simple form can check that its required values are not blank:

const requiredValues = ["Likhon", "likhon@example.com", "Hello"];

const allFieldsFilled = requiredValues.every((value) => value.trim() !== "");

console.log(allFieldsFilled); // true

trim() removes spaces from the beginning and end, so a value containing only spaces counts as blank. every() stops and returns false as soon as it finds a value that fails the condition. If all values pass, it returns true.

This is only a small presence check. A real form may also need to check email format, length, and other rules, and the server should validate submitted data too.

The key difference at a glance

MethodReturns true when…Example question
some()At least one item passes the callbackDoes any role equal "admin"?
every()Every item passes the callbackIs every required value non-empty?

Both methods receive a callback much like filter(). The callback can also receive an item's index and the original array:

items.some((item, index, array) => {
  // Return true when this item matches
});

Empty arrays: a useful detail

For an empty array, some() returns false: there is no item that passes. every() returns true: there is no item that fails. This is the standard logical meaning of “at least one” and “all,” but it can surprise you. If an empty list should be invalid in your situation, check its length as well:

const allChecksPass = checks.length > 0 && checks.every(isValid);

Try it yourself

Use some() to find out whether any score is below 50. Then use every() to check whether all scores are at least 50.

const scores = [82, 67, 49, 91];

What does each method return? Try changing 49 to 59 and check again.

Recap

  • Use some() to test whether at least one element matches.
  • Use every() to test whether all elements match.
  • Both return a Boolean and stop as soon as the answer is known.
  • On an empty array, some() returns false and every() returns true.

What's next?

Next, we’ll look at JavaScript destructuring, spread, and rest operators, including examples used with React.

Written by

Dev Likhon

Dev Likhon

Full Stack Developer & Tech Educator

6+ years of professional development experience.

About Me
Share:

Comments

Share what you learned or ask a question.

Loading comments...