The Array.prototype.every() method always returns true for an empty array
My friend Nicholas Zakas made an interesting observation on Mastodon this week: the Array.prototype.every()
method always returns true
for an empty array.
let arr = [];
// Returns true
arr.every(function (item) {
return false;
});
// Also returns true
arr.every(function (item) {
return true;
});
Axel Rauschmayer provided an interesting explanation…
.every()
implements universal quantification (“for all”, ∀). And that operator is always true for empty sets…That is also how we understand [for all] intuitively if we use it in natural language: “All humans that live on Venus have blue skin” is true (because no humans live on Venus).