Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

[].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.

like image 864
Saul does Code Avatar asked Dec 07 '15 15:12

Saul does Code


People also ask

Why is empty array true?

Because Array is type of object , the fact that an empty Array is conversed to true is correct.

What does the array every method return when passed an empty array?

When invoked on an empty array, every() returns true . The return value of predicate is interpreted as a boolean value. true and all truthy values indicate that the array element passes the test or meets the condition described by that function.

Why empty array is true in JavaScript?

Values not on the list of falsy values in JavaScript are called truthy values and include the empty array [] or the empty object {} . This means almost everything evaluates to true in JavaScript — any object and almost all primitive values, everything but the falsy values.

Which array prototype function returns a reference to a new array every time?

Definition and Usage. The every() method executes a function for each array element. The every() method returns true if the function returns true for all elements.


4 Answers

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.

like image 81
Draco18s no longer trusts SE Avatar answered Oct 18 '22 16:10

Draco18s no longer trusts SE


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.

like image 23
apsillers Avatar answered Oct 18 '22 17:10

apsillers


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.

like image 42
djechlin Avatar answered Oct 18 '22 17:10

djechlin


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

like image 23
epascarello Avatar answered Oct 18 '22 17:10

epascarello