[X]

One Object Equals One, Two and Three

luljs 2018 still works

One object can satisfy a == 1 && a == 2 && a == 3 at the same time.

In 2026: It still returns true. Loose == coerces the object by calling valueOf, and this valueOf returns 1, then 2, then 3 as it increments a counter. Each comparison reads the next value, so all three hold in a single expression.

Where it came from: A 2018 Stack Overflow question, among the highest-voted JavaScript questions on the site. MDN

<script>
var a = { i: 1, valueOf: function(){ return this.i++; } };
document.write("<p><code>a == 1 && a == 2 && a == 3</code> is <b>" + (a == 1 && a == 2 && a == 3) + "</b></p>");
document.write("<p>Each <code>==</code> calls valueOf, and valueOf hands back 1, then 2, then 3.</p>");
</script>
sandboxed demo · breaks nothing but itselfrestart

More in luljs

all 63 in luljs › · the whole library ›