[X]

Truthy Yet Equal To false

luljs 2011 still works

An empty array passes an if test yet is loosely equal to false at the same time.

In 2026: An if test asks whether the value is truthy, and every object including [] is truthy, so the block runs. Loose == takes a different path: it turns [] into an empty string, then into the number 0, and false is also 0, so [] == false is true. One value, two operators, two opposite answers.

Where it came from: Kyle Simpson, You Don't Know JS Yet: Types & Grammar, the Coercion chapter (falsy objects versus loose equality). MDN

<script>
document.write('<p><code>[] ? "truthy" : "falsy"</code> is <b>' + ([] ? 'truthy' : 'falsy') + '</b></p>');
document.write('<p><code>[] == false</code> is <b>' + ([] == false) + '</b></p>');
document.write('<p>if() asks whether the value is truthy, == turns [] into the number 0. The two rules disagree.</p>');
</script>
sandboxed demo · breaks nothing but itselfrestart

More in luljs

all 63 in luljs › · the whole library ›