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.6k stars 2.54k forks source link

Incorrect explanation #275

Open AbdSat opened 1 year ago

AbdSat commented 1 year ago

However, the >= operator in fact works in a very different way, which is basically to take the opposite of the < operator

That's not ture, >= and <= operators just convert both sides to numbers if they have different types and that's why null >= 0 returns true because 0 == 0 is true.

You can test that withNaN and will return false :

NaN == 0 // false
NaN > 0 // false

// But ...
NaN >= 0 // false

You can see that NaN < 0 returns false and >= operator doesn't return the opposite.

So >= and <= operators are just shorthands : x >= yx > y || x == y x <= yx < y || x == y and if x and y have different types they will be converted to numbers before comparing.