Why Does .All? Return True on an Empty Array

Why does Array.prototype.every return true on an empty array?

See the docs

every acts like the "for all" quantifier in mathematics. In
particular, for an empty array, it returns true. (It is vacuously true
that all elements of the empty set satisfy any given condition.)

As an edit, because I looked Vacuous truth up. I understood it from context, but I was interested in the formal definition. This paraphrased quote exemplifies the meaning:

"You are my favorite nephew" is a vacuous statement if he is the only nephew: there are no others to consider.

Why do empty JavaScript arrays evaluate to true in conditional structures?

From http://www.sitepoint.com/javascript-truthy-falsy/

The following values are always falsy:

  • false
  • 0 (zero)
  • 0n (BigInt zero)
  • "" (empty string)
  • null
  • undefined
  • NaN (a special Number value meaning Not-a-Number!)

All other values are truthy, including "0" (zero in quotes), "false" (false in quotes), empty functions, empty arrays ([]), and empty objects ({}).

Regarding why this is so, I suspect it's because JavaScript arrays are just a particular type of object. Treating arrays specially would require extra overhead to test Array.isArray(). Also, it would probably be confusing if true arrays behaved differently from other array-like objects in this context, while making all array-like objects behave the same would be even more expensive.

Why does .all? return true on an empty array?

In Ruby you can never loop over an empty collection (array, hashes, etc.), so in your case your block never gets executed. And if the block never gets executed, all? returns true (there is no condition to make the result false).

Read about all? in the Ruby documentation.

You can simply achieve your goal by

  !array.empty? && array.all? { |value| value == 2 }

Why don't Array.prototype.some() return true for an empty set?

With .every, you're asking whether all items in a list fulfil a condition. If that list is empty, well, there are no items which do not fulfil the condition. Every item that there is (which is none) fulfils the given condition, so .every is vacuously true.

With .some, you're asking whether any item in a list fulfils a condition. If there are no items, then there's no item to fulfil the condition. So no, nothing in an empty list can fulfil the condition and it's vacuously false.

Why does Enumerable.All return true for an empty sequence?

It's certainly not a bug. It's behaving exactly as documented:

true if every element of the source sequence passes the test in the specified predicate, or if the sequence is empty; otherwise, false.

Now you can argue about whether or not it should work that way (it seems fine to me; every element of the sequence conforms to the predicate) but the very first thing to check before you ask whether something is a bug, is the documentation. (It's the first thing to check as soon as a method behaves in a way other than what you expected.)

Calling `every` and `some` methods of an empty array returns weird results

"every" returns true if every element of an array passes a test. If there are no items in the array, "every" element in the array passes the test.

"some" returns true if at least one element of an array passes a test. If the array is empty, none of the elements pass the test and it returns false.

It looks like, what JS interpreter really does is that Boolean([]) (in the first situation). And that of course returns true. Maybe that is correct?

With an empty array, neither every nor some call the callback at all because there is nothing to test. You can check this with:

[].every(() => console.log("this never prints"));
[].some(() => console.log("this never prints"));

Is there a method like .every() for arrays with empty values?

If there is no element in the array, every element in the array fullfills the condition. Therefore it returns true. To achieve the opposite:

arr.length && arr.every(/*...*/)


Related Topics



Leave a reply



Submit