This is something I stumbled across a while ago. Someone on the internet said he was asked this question during an job interview:
Can you think of a situation where this is true:
a == 1 && a == 2 && a == 3
Well, yes.
You have to ask the question, what a
is.
Is it a Number
? Obviously not.
The solution is quite simple if you know how JavaScript works.
You can define a
to be an Object
and overwrite .valueOf
:
const a = {
i: 1,
valueOf: function() {
return this.i++;
}
};
if (a == 1 && a == 2 && a == 3) {
console.log('yeah');
}
I think this is very awkward!
Or like someone else on the internet said:
Sometimes when I’m writing Javascript I want to throw up my hands and say “this is bullshit!” but I can never remember what “
this
” refers to.
So long!