The JavaScript Interview Question That Separates the Nerds from the Hired
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