Course contentsShow
← JavaScript
Lesson 877 of 2,72320. Array Iteration and TransformationFree lesson

Empty Array Edge Cases for every() and some()

Learn that every() returns true for empty arrays while some() returns false, and why this matters.

Empty Array Edge Cases for every() and some()

You'll learn how every() and some() behave differently on empty arrays—and why these edge cases matter for writing correct code.

The Surprising Truth About Empty Arrays

When you call every() or some() on an empty array, they return opposite values:

  • every() returns true for an empty array
  • some() returns false for an empty array
const emptyArray = [];

console.log(emptyArray.every(x => x > 10)); // true
console.log(emptyArray.some(x => x > 10));  // false

Why This Behavior Makes Sense

Think of every() as asking: "Are there any elements that fail my condition?" For an empty array, there are no failing elements, so the answer is true. This is called vacuous truth in logic.

Think of some() as asking: "Is there at least one element that passes my condition?" For an empty array, there are no passing elements, so the answer is false.

Why This Matters in Real Code

These edge cases can cause bugs if you're not careful:

function allItemsExpensive(items) {
  return items.every(item => item.price > 100);
}

allItemsExpensive([]); // true — careful! No items doesn't mean "all expensive"

If you're validating user input or checking business rules, you might need to explicitly check for empty arrays first:

function allItemsExpensive(items) {
  if (items.length === 0) return false; // or handle appropriately
  return items.every(item => item.price > 100);
}

Key Takeaway: every() returns true for empty arrays (vacuously true), while some() returns false (nothing matches). Always consider whether you need special handling for empty array cases in your logic.