[].every(i => i instanceof Node) // -> true

Why does the every method on arrays in JavaScript return true when the array is empty. I'm trying to do type assertion like so...

const isT = (val, str) => typeof val === str
const nT = (val, str) => !isT(val, str)
const is = {}

is.Undef = (...args) => args.every(o => isT(o, 'undefined'))
is.Def = (...args) => args.every(o => nT(o, 'undefined'))
is.Null = (...args) => args.every(o => o === null)
is.Node = (...args) => args.every(o => o instanceof Node)
is.NodeList = (...args) => args.every(n => n instanceof NodeList)

but these still return true even when no arguments are passed to them.

  • 1
    don't know the answer but you should be able to use is.Undef = (...args) => args.length && args.every(o => isT(o,'undefined')); – synthet1c Dec 7 '15 at 15:32
up vote 7 down vote accepted

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.

  • 1
    That's kind of a misleading example because favorite here means "1st out of 1." Vacuous tends to imply "all out of 0." If you worded it more verbosely, "I like you more than every nephew besides you," it is vacuous since there is no nephew besides "you". – djechlin Dec 7 '15 at 15:46
  • 1
    "I like every newphew besides you less." – Draco18s Dec 7 '15 at 15:48
  • Yes, that is clear. Note it is also true that "I like every nephew besides you more." It turns out that "you are both my favorite and least-favorite nephew," which is only slightly strange. – djechlin Dec 7 '15 at 15:50
  • 1
    Just as the statement "every cellphone in the empty room is on and off, simultaneously" is. I suppose it is the antithesis of null always comparing to false: null is definitely not something, but an empty set on the other hand is definitely not not-something. (that is: math is weird). – Draco18s Dec 7 '15 at 15:54

It's more mathematically valid to say "every" is - vacuously - true if there are no elements.

You need it so the relationship "for all x, P" is the same as "NOT(there exists x such that not P)".

It's somewhat a matter of convention but it does "make the math work out nicely" quite often.

MDN Array every()

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.)

From the ECMAScript specification of Array.prototype.every (bold emphasis mine):

every calls callbackfn once for each element present in the array, in ascending order, until it finds one where callbackfn returns false. If such an element is found, every immediately returns false. Otherwise, if callbackfn returned true for all elements, every will return true.

[...] every acts like the "for all" quantifier in mathematics. In particular, for an empty array, it returns true.

Considering the first bolder phrase above: since every finds no elements for which the callback return false (because the callback never even runs, because there are no elements), it returns true, as confirmed by the second bolded phrase.

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.