5

Quick javascript question:

let a = .............
Make a == a always return false.

What would a be? One solution is to let a = NaN. Anything fancier? :>

Comments
  • 4
    I remember reading a question on stackoverflow, "can a == 51 && a == 52 && a == 53 be true?". Yes it can, and the javascript version included overriding a special method of a and incrementing a after each equality check. Maybe this can be done here too.
  • 3
  • 1
    Yeah, another interesting question, but's it's a different case.

    To have a "a==1 && a==2 && a==3" you can change the valueOf property using Object.defineProperty (check the screenshot).

    But that's leveraging the fact that when checking for loose equality (coercion permitted) between an object and a primitive value, js will use the the valueOf property to get the object's primitive value.

    But now we're checking if a == a, where a may not be a primitive type!
  • 1
    I think symbols behave like this
Add Comment