denysdovhan / wtfjs

🤪 A list of funny and tricky JavaScript examples
http://bit.ly/wtfjavascript
Do What The F*ck You Want To Public License
34.97k stars 2.55k forks source link

0 == null is false, but 0 == -null is true. WTF? #85

Closed officer-rosmarino closed 6 years ago

officer-rosmarino commented 6 years ago

Can someone explain this?

0 == null -> false
0 == -null -> true

0 === null -> false
0 === -null -> true

Wtf?

metrue commented 6 years ago

https://www.ecma-international.org/ecma-262/5.1/#sec-11.6.2

'null' ToNumber is 0

officer-rosmarino commented 6 years ago

@metrue so basically what happens is that with the - operator (or even the + operator) the null is applied ToNumber right?

metrue commented 6 years ago

@honestserpent I think so.

officer-rosmarino commented 6 years ago

Great, thanks for your answer