Skip to content
DevMeme
4969 of 7590
Interviews Post #5436 · source on Telegram

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

26
Anonymous ★ Top Pick Yes, this evaluates to true if 'a' is a proxy for the project manager's requirements, which change every time you look at them
  1. Anonymous ★ Top Pick

    Yes, this evaluates to true if 'a' is a proxy for the project manager's requirements, which change every time you look at them

  2. Anonymous

    If your codebase relies on that trick in production, congratulations - you just implemented Schrödinger’s boolean

  3. Anonymous

    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.'

  4. Anonymous

    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'?

  5. Anonymous

    Yes - add a Symbol.toPrimitive that increments; the chain goes true and ESLint’s eqeqeq screams louder than your pager during a Friday deploy

  6. Anonymous

    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

  7. Anonymous

    Yes, chains to a=3 (truthy) - the trivia interviewers wield like a senior dev's ignored architecture diagram

  8. @theu_u 2y

    Overriding equality operator goes brrrr

    1. @sylfn 2y

      overloading "and" operator:

    2. @deadgnom32 2y

      overriding type conversion

  9. @mr_adolina 2y

    can someone explain for the back of the class?

  10. @qtsmolcat 2y

    https://javascript.plainenglish.io/interviewer-can-a-1-a-2-a-3-ever-evaluate-to-true-in-javascript-d2329e693cde

  11. @qtsmolcat 2y

    Paywall tho fyi

    1. @paul_thunder 2y

      hilarious.. who are those people who are paying for THAT??🤣

      1. @qtsmolcat 2y

        Medium has always had paywalled content

        1. @paul_thunder 2y

          but not all of it. that is the point. Can it possibly be worth to pay for "some paywalled content"? I doubt it

          1. @qtsmolcat 2y

            Depends on how much you value that content ig

  12. @TedK777 2y

    Wtf

  13. @HellishGuardian 2y

    let a = { i: 1, valueOf: function() { return this.i++; } };

  14. @Araalith 2y

    It can in any modern language. 1. Getters 2. User-defined casting or conversion operators. 3. Comparison override.

  15. @misesOnWheels 2y

    https://stackoverflow.com/questions/48270127/can-a-1-a-2-a-3-ever-evaluate-to-true#48270314

    1. @Mikle_Bond 2y

      Ok, the ZWNJ and ZWJ approach actually got me)

  16. @HellishGuardian 2y

    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.

  17. @SamsonovAnton 2y

    Because it can! 😎

  18. @cTuxo 2y

    it's even possible to do with (a === 1 && a === 2 && a === 3)

  19. @dvkov 2y

    JS interview questions always scare me

Use J and K for navigation