HiveBrain v1.2.0
Get Started
← Back to all entries
snippetjavascriptTip

What happens when you call every() on an empty JavaScript array?

Submitted by: @import:30-seconds-of-code··
0
Viewed 0 times
emptyjavascriptwhatwhenhappensyoueverycallarray

Problem

A few days ago, I stumbled upon a perplexing piece of JavaScript behavior. I was working on a project that involved checking if all elements in an array met a certain condition. Naturally, I used Array.prototype.every() to accomplish this task.
However, I noticed that when I called Array.protoype.every() on an empty array, it returned true. This behavior didn't necessarily seem wrong, but it made me wonder. _Had I done something wrong? Should I be checking for an empty array every time I call this method from here on out?_
The official MDN documentation, that is linked above, states the following in regards to this scenario:
> 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.)
I didn't find this answer satisfying, but, hey, at least it's documented!

Solution

As you can see, the method will return `true` if there are no elements to iterate over, even if the predicate function always returns `false`! I like to think that this is a more intuitive explanation for this behavior.

As a matter of fact, the [ECMAScript specification for `Array.prototype.every()`](https://tc39.es/ecma262/#sec-array.prototype.every) pretty much describes the same steps, albeit with a few more details that are taken care of.

### Extending the logic

We can extend this logic to `Array.prototype.some()` as well. As soon as one element returns `true`, the method will **short-circuit** and return `true`. If there are no elements to iterate over, the method will return `false`. Thus, the initial and **default** value of the method should be `false`.

Again, a `for` loop analogy can help clarify this:


The official MDN documentation, that is linked above, states the following in regards to this scenario:
> 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.)
I didn't find this answer satisfying, but, hey, at least it's documented!
But that made me wonder, how does Array.prototype.some() behave in this scenario? Does it return true as well? Well...
> some() acts like the "there exists" quantifier in mathematics. In particular, for an empty array, it returns false for any condition.
Interesting, so these two methods behave opposite when it comes to empty arrays.

Code Snippets

As you can see, the method will return `true` if there are no elements to iterate over, even if the predicate function always returns `false`! I like to think that this is a more intuitive explanation for this behavior.

As a matter of fact, the [ECMAScript specification for `Array.prototype.every()`](https://tc39.es/ecma262/#sec-array.prototype.every) pretty much describes the same steps, albeit with a few more details that are taken care of.

### Extending the logic

We can extend this logic to `Array.prototype.some()` as well. As soon as one element returns `true`, the method will **short-circuit** and return `true`. If there are no elements to iterate over, the method will return `false`. Thus, the initial and **default** value of the method should be `false`.

Again, a `for` loop analogy can help clarify this:

Context

From 30-seconds-of-code: empty-array-every-some

Revisions (0)

No revisions yet.