The JavaScript Interview Question That Separates the Nerds from the Hired
Why is this Interviews meme funny?
Level 1: The Changing Number Trick
Imagine a magician who has three different hats and switches which hat he's wearing every time you ask him a question. First you ask, "Are you wearing the red hat?" and he quickly puts on the red hat and says, "Yes!" Next you ask, "Are you wearing the blue hat now?" he swaps to the blue hat in a flash and again says, "Yes." Finally, you ask about the green hat, and before you finish the question he's already wearing the green hat: "Yes!" It seems like one person is somehow wearing all three hats at once, but really he's just switching super fast to fool you.
This meme is funny for the same reason. The programming trick here is like that magician's stunt. We have one thing in the code (a variable named a) that cleverly changes its "disguise" each time it's checked, so it manages to say "I'm 1," then "I'm 2," then "I'm 3" at just the right moments. Normally, one person can't literally be three different people at the same time, and one number can't equal three different numbers either. But with a sneaky approach, our variable a pulls a magic trick: it changes itself in the blink of an eye whenever someone looks at it. It's a silly little illusion in the programming world that makes us laugh because it breaks the normal rules in a surprising way. The humor comes from that "aha!" moment – discovering that JavaScript can pull off this stunt, even though at first it sounds impossible. It's like seeing a clever illusion and thinking, "Wow, I didn't know you could do that!"
Level 2: Type Coercion Magic
Let's break this down in simpler terms. In JavaScript, the double equals == is the loose equality operator. "Loose" means JavaScript will try to convert the values to a common type when comparing them. On the other hand, the triple equals === is the strict equality operator, which means "no conversions, just check if they're exactly the same type and value." New developers quickly learn that using === is safer because == can do some weird behind-the-scenes magic.
Type coercion is that behind-the-scenes magic of conversion. For example, if you do '5' == 5, JavaScript will coerce the string '5' into the number 5 and then compare, resulting in true. It feels a bit like JavaScript saying, "I'll help you out by guessing that you meant the number 5." Sometimes this guesswork leads to surprising results. You might have seen odd cases like true == 1 (which is also true, because true becomes the number 1). These are classic quirks that can trip you up if you're not careful.
Now, what about an object like a? When you compare an object to a number using ==, JavaScript tries to convert the object into a primitive value (a number or a string) so it can compare. It does this by calling special object methods like valueOf() or toString() on that object. Normally, if an object doesn't have its own valueOf defined, it just inherits a default that isn't anything special. But if we define a custom valueOf for our object a, we get to decide what number a should turn into whenever it's used in a comparison or arithmetic.
That's the valueOf override trick: we make a.valueOf() return a different number each time it's called. The loose equality checks a == 1, a == 2, and a == 3 will each trigger a separate call to a.valueOf(). By coding valueOf to give 1 on the first call, 2 on the second, and 3 on the third, we can fool JavaScript into thinking a is equal to 1, then 2, then 3 in sequence. Another method (which you might see in solutions to this interview question) is using a getter: for example, define a as a global variable with a custom getter that increments a counter every time a is accessed. Both methods achieve the same result: a acts like a chameleon, changing its value each time it's checked.
For a junior developer, the key takeaway is that loose equality (==) allows type coercion, which means JavaScript will bend types to try to make a comparison work. This can lead to unexpected outcomes, like this crazy example where one variable seems to equal multiple different numbers. In a normal situation, you would never write code that does this – it's more of a party trick to show off JavaScript's oddities. Interviewers bring it up not because you'd ever need to do this in real life, but to see if you understand how JavaScript handles comparisons and to spark a discussion about == vs ===. Once you realize how objects can define their own conversion behavior, it makes sense how a could be 1, 2, and 3 at different moments. Pretty wild, right?
Level 3: Double Equals, Triple Trouble
It's the classic interview gotcha question that both puzzles and delights seasoned JavaScript developers. The interviewer asks whether something as seemingly impossible as (a == 1 && a == 2 && a == 3) could ever evaluate to true. If you're experienced with JavaScript's language quirks, you probably smirked: Of course it can, thanks to JavaScript's loose equality sorcery! This meme highlights a famous type coercion brain-teaser demonstrating how far JavaScript's flexibility can go.
Why is this funny? Because it's like a trap: anyone thinking in normal programming logic would immediately say "No way, one variable can't equal 1, 2, and 3 at the same time!" But in JavaScript, with its notorious loose equality operator (==), all bets are off. This scenario relies on tweaking how the variable a behaves under the hood. By overriding a's internal conversion methods (like valueOf() or using a custom getter), we can make a act like a shape-shifter. Each time a is compared with a number, it can give a different value. Sneaky, right?
Let's see how it works. We can define a as an object that changes its value every time it's coerced to a number:
const a = {
valueOf: function() {
// This is called when JavaScript needs a number from 'a'
if (!this._value) {
this._value = 1;
}
return this._value++;
}
};
console.log(a == 1 && a == 2 && a == 3); // true
In this code, a.valueOf() returns a number that starts at 1 and increments each time it's asked for a value. The expression a == 1 calls a.valueOf() and gets 1 (so that part is true). Then a == 2 calls valueOf() again on the same object, which now yields 2 (true again!). By the time we check a == 3, the internal counter has advanced to 3, making the third comparison true as well. The result: the whole chained expression returns true. We've exploited the rules of loose equality and type coercion in JS to pull off this trifecta of truth.
Seasoned developers recognize this as a bit of interview humor and a nod to JavaScript's dynamic nature. It's a perfect example of JavaScript's equality weirdness. Under the hood, the JavaScript engine's equality algorithm attempts to convert objects to primitives when using ==. By providing a custom conversion (like our valueOf trick above, or defining a with a special getter via Object.defineProperty on the global object), we override how a behaves. Essentially, a becomes a trickster that lies about its value differently each time it's asked.
The meme gets a laugh (or an eyeroll) because it showcases a contrived scenario that shouldn't be possible — but JavaScript lets it happen. It's a reminder that == does coerce types and can produce some really weird results. Every senior JavaScript dev has learned (perhaps the hard way) to avoid these pitfalls by using strict equality (===) whenever possible. If you tried (a === 1 && a === 2 && a === 3) with the same setup, it would fail immediately, because === checks type and doesn't invoke any conversion shenanigans. That's why one common JavaScript best practice is "always use === instead of ==" unless you truly understand the side effects of TypeCoercion.
So, when an interviewer pulls out this question, it's both a test of your knowledge of the language's odd corners and a bit of shared comic relief. The answer, as any JS old-timer will tell you with a grin, is “Yes, it can be true — in JavaScript, of course!”
Description
This image is a screenshot of a blog post title, likely from a platform like Medium, as indicated by the 'Member-only story' text in the upper-left corner. The main text, in a large, bold, black font, poses a classic JavaScript interview question: 'Interviewer: Can (a==1 && a==2 && a==3) Ever Evaluate to ‘true’ in JavaScript?'. This question is a well-known 'gotcha' used in technical interviews to test a developer's deep understanding of JavaScript's more obscure features, specifically type coercion and how objects behave with the loose equality (`==`) operator. The expression can, in fact, be made to evaluate to true by defining a variable `a` as an object with a custom `valueOf` or `Symbol.toPrimitive` method that returns an incrementing value each time it's accessed. While it demonstrates knowledge of the language's internals, its value as a practical interview question is often debated, as it tests trivia over problem-solving skills
Comments
26Comment deleted
Yes, this evaluates to true if 'a' is a proxy for the project manager's requirements, which change every time you look at them
If your codebase relies on that trick in production, congratulations - you just implemented Schrödinger’s boolean
The real test isn't whether you know about valueOf shenanigans - it's whether you'd actually approve code that relies on this behavior in production, or if you'd immediately open a P0 ticket titled 'Fire whoever wrote this.'
Ah yes, the classic 'let me test if you know JavaScript is held together with duct tape and prayer' interview question. The answer involves abusing valueOf() or a getter that increments on each access - because nothing says 'production-ready engineer' like knowing how to make a variable lie about its own identity three times in a row. This is the technical equivalent of asking a structural engineer if they can make a building that's simultaneously 1, 2, and 3 stories tall. Sure, you *can* do it with enough JavaScript dark magic, but the real question is: should we hire the person who asks this, or the person who responds with 'I'd refactor this code in the PR review'?
Yes - add a Symbol.toPrimitive that increments; the chain goes true and ESLint’s eqeqeq screams louder than your pager during a Friday deploy
Sure - give a stateful [Symbol.toPrimitive] so a returns 1, then 2, then 3; it’ll make the expression true and your team’s ESLint eqeqeq rule even truer
Yes, chains to a=3 (truthy) - the trivia interviewers wield like a senior dev's ignored architecture diagram
Overriding equality operator goes brrrr Comment deleted
overloading "and" operator: Comment deleted
overriding type conversion Comment deleted
can someone explain for the back of the class? Comment deleted
https://javascript.plainenglish.io/interviewer-can-a-1-a-2-a-3-ever-evaluate-to-true-in-javascript-d2329e693cde Comment deleted
Paywall tho fyi Comment deleted
hilarious.. who are those people who are paying for THAT??🤣 Comment deleted
Medium has always had paywalled content Comment deleted
but not all of it. that is the point. Can it possibly be worth to pay for "some paywalled content"? I doubt it Comment deleted
Depends on how much you value that content ig Comment deleted
Wtf Comment deleted
let a = { i: 1, valueOf: function() { return this.i++; } }; Comment deleted
It can in any modern language. 1. Getters 2. User-defined casting or conversion operators. 3. Comparison override. Comment deleted
https://stackoverflow.com/questions/48270127/can-a-1-a-2-a-3-ever-evaluate-to-true#48270314 Comment deleted
Ok, the ZWNJ and ZWJ approach actually got me) Comment deleted
Every time you try to compare the object to a number, this method will be called, and you can change the object's internal state so that it returns a different value each time. Comment deleted
Because it can! 😎 Comment deleted
it's even possible to do with (a === 1 && a === 2 && a === 3) Comment deleted
JS interview questions always scare me Comment deleted