Recent chats
ASKGEEZ Q&A GUIDETechnology & devices

How can I check whether every or any array item meets a condition?

Quick answer

Use array.every(predicate) to ask whether every visited item passes a condition; it returns true only when they all do. Use array.some(predicate) to ask whether at least one visited item passes; it returns true when it finds a match. For example, with [1, 2, 3] and an even-number predicate, every() is false while some() is true. [S1] [S2]

LET’S WORK THROUGH IT

The question, the details, the next step.

Question

I have an array and a condition for its items. How do I check whether the condition applies to all items or just one or more?

Geez

Use every() for the all-items question and some() for the at-least-one question. Both take a predicate—a function that returns a truthy or falsy result for an item—and produce a boolean. every() is true only if the predicate passes for every visited item; some() is true if it passes for at least one visited item. [S1] [S2]

For instance, suppose the condition is “is this number even?” You can write it once as const isEven = n => n % 2 === 0;, then pass it to either method. The predicate says what to check; the method says whether you mean all or any.

Follow-up question

Can you show both methods on the same array and condition, including the results?

Geez

Here is the paired example using the same array and predicate:

const nums = [1, 2, 3];

const isEven = n => n % 2 === 0;

nums.every(isEven); // false

nums.some(isEven); // true

every() is false because 1 and 3 do not pass the even-number test. some() is true because 2 does pass it. So the two results are not contradictory: they answer different questions. When every value is even, as in [2, 4, 6], every(isEven) is true. [S1] [S2]

Follow-up question

What if all the items match? And how should I make the code easy to understand later?

Geez

With [2, 4, 6] and the same isEven predicate, every() returns true, because all three items pass. some() also returns true, because at least one passes—in this case, all three do. [S1] [S2]

For readable code, name the predicate after the condition, then choose the method that matches the question:

const allAreEven = nums.every(isEven);

const hasAnEvenNumber = nums.some(isEven);

Those variable names make the intended meaning clear where you use the result. If the condition is more involved, a descriptive predicate name can keep the check understandable without repeating its logic.

Follow-up question

Is there anything surprising about an empty array or how many items get checked?

Geez

Yes. On an empty array, every() returns true, while some() returns false. That can surprise readers, so if an empty array should count as a failure in your situation, check that it has items separately and make the intended rule explicit. [S1] [S2]

You also do not need to assume the predicate runs for every item: every() stops once an item fails, and some() stops once an item passes. Sparse-array empty slots are not visited by either method. [S1] [S2] For ordinary checks, focus on which question you mean—“all?” or “at least one?”—rather than relying on the methods to examine every position.

A practical checklist

  • Write the condition as a predicate that returns a truthy or falsy result for one item.

  • Use every(predicate) when the requirement is that all visited items pass.

  • Use some(predicate) when one or more matching items are enough.

  • Test your intended case and read the boolean result against the question you asked.

  • Decide explicitly what an empty array should mean in your application.

A few more useful answers

Can `every()` and `some()` both return true for the same array?

Yes. If all items pass the predicate, then at least one passes too, so both return true. For example, with [2, 4, 6] and an even-number predicate, both are true. [S1] [S2]

Does `some()` mean exactly one item matches?

No. It means at least one item matches; it can be one item or several. Use a different counting approach if you need to know how many match.

What do these methods return for an empty array?

every() returns true and some() returns false on an empty array. If that does not fit your rule, handle the empty case explicitly. [S1] [S2]

Sources

References used for this guide.

  1. [S1] Array.prototype.every() - JavaScript | MDN
    developer.mozilla.org · Checked
  2. [S2] Array.prototype.some() - JavaScript | MDN
    developer.mozilla.org · Checked

YOUR QUESTION. YOUR DETAILS.

Let’s work through your situation.

Get a tailored answer or help with your next task. Starts at $1, with a quote before you pay.

Ask your own question
← Back to Geez at Work