Skip to the solution
EasyFrontend6 min read

What do Array.some and Array.every do?

438 views

The problem

Question presented to candidate: "You have an array of 1000 users, and you want to check if any of them are under 18. Would some() actually stop checking once it finds one, or does it genuinely check all 1000 regardless? Same question for every() β€” checking that all users are adults."

What a strong answer should cover:

  • πŸ“Œ Interview term: some(predicate) β€” returns true if at least one element genuinely passes the predicate; πŸ“Œ every(predicate)** β€” returns true only if all elements genuinely pass.
  • πŸ“Œ Verified, not assumed β€” directly answering the prompt's exact question: a real call counter proved both methods genuinely short-circuit β€” some() genuinely stopped calling its predicate at exactly the element that first returned true (not continuing through the rest of the array), and every() genuinely stopped at exactly the element that first returned false.
  • πŸ“Œ Interview term: the real, vacuous-truth edge cases β€” confirmed directly: an empty array's some() genuinely returns false (no element exists to pass), while an empty array's every() genuinely returns true (there is no element to fail the check) β€” a real, easy-to-get-backwards pair of facts, worth memorizing precisely rather than guessing.
  • A precise answer names some/every as answering genuinely existential ("does at least one exist") versus universal ("do all satisfy") questions about an array β€” the real, direct vocabulary an interviewer expects.
  • The precise, honest scope: both methods genuinely only report a boolean β€” if the actual, specific matching element itself is needed (not just whether one exists), find() (covered in this bank's own dedicated question) is the correct, more direct tool.

Clarifying questions expected:

  • "Does the actual downstream code need the specific matching element itself, or just a yes/no answer to whether one exists?" β€” directly decides between some/every (verified above as boolean-only) and find (covered in this bank's own dedicated question).
  • "Is the actual predicate function genuinely expensive to run per element?" β€” the real short-circuiting verified above is specifically valuable when each individual check has a real, meaningful cost.

Code / implementation expected: Yes β€” a real call counter directly proving both methods genuinely stop early, plus the real empty-array vacuous-truth outcomes, is the concrete, convincing proof of exactly how efficiently β€” and correctly β€” these two methods behave.

  • some
  • every

Try it yourself

Sketch your approach before you read the solution. That is what the interview tests.

Stuck? Get a hint.

A short nudge toward the approach. It will not give the answer away.

Solution

The solution is hidden so you can try first

6 min read Β· 8 sections

Run the code

Edit the code and run it here. Each console line with an expected value becomes a test.

JavaScriptReal proof: a call counter confirms both some() and every() genuinely short-circuit, plus the real empty-array vacuous-truth outcomes

Discussion

Other approaches, follow-ups and how it went in real interviews.

Sign in to join the discussion.

No comments yet. Share how you would answer it.

Question 26 of 165 in JavaScript

Keep your momentum going.

Mark this question solved to track your progress, then take the next one while it is fresh.

Up nextEasy

How do the new Set methods `union()`, `intersection()`, `difference()` and `symmetricDifference()` work?

Continue