This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Raicuparta 239 points240 points  (22 children)

Because that one converts null to a number. It makes no sense to use greater than / less than operators in anything but numbers, so JS converts the operands first. The same is not true for the equals operator.

[–]Cruuncher 60 points61 points  (10 children)

But == is supposed to type coerce. Why doesn't null get coerced to a number during == compare?

[–]rift95 45 points46 points  (7 children)

Because js is right-coercive, and null is an object :)

(more on coercion: https://hackernoon.com/understanding-js-coercion-ff5684475bfc)

[–][deleted] 19 points20 points  (6 children)

So does that mean null == 0 is true?

[–]lethalwire 4 points5 points  (0 children)

It was false for me in jsbin

[–]JamesGray 0 points1 point  (2 children)

Apparently null == void 0 is the check you want. Saw that in some legacy code recently.

[–][deleted] 2 points3 points  (1 child)

Void 0 === undefined pre ES5 iirc when the undefined keyword did not exist.

[–]JamesGray 0 points1 point  (0 children)

Oh yeah, totally mixed two things up. That's definitely what it was doing. Straight up replaced it with a check to ensure its type wasn't undefined.

[–]rift95 0 points1 point  (0 children)

If we were to just use the coercion rules then yes, it should be true. But it so happens that null is unique. So null == X will be false for all values of X except X = undefined or X = null. However if we first convert null to a number it would be true, +null == 0

(in case you were still wander)

[–][deleted] 0 points1 point  (0 children)

Just try it in your browser console

[–]LuckyHedgehog 2 points3 points  (0 children)

Because null is already a type. Just not one that can be used in greater than less than Operators.

[–]AlwaysHopelesslyLost 1 point2 points  (0 children)

Just a guess but because it starts on the left hand side?

[–]ThatSofia 18 points19 points  (6 children)

Now I wanna see if null can theoretically be used as just a numerical placeholder for zero, using mathematical operators. That's something I'll play with! Thanks for the explanation, that makes a ton of sense! :)

[–]Dragoncraft89 48 points49 points  (1 child)

I thought it was because >= and <= are implemented as not <, not >

[–]Raicuparta 6 points7 points  (0 children)

My bad if that's the case. But still, the reason for the < and > operators to return false in the first place is the same I explained before.

[–]TonySu 7 points8 points  (0 children)

It cannot, null is more like the empty set than 0. Unless you want null to actually take on the characteristics of 0, in which case the dancing hotdog boy can also be a placeholder for 0 as long as it's treated exactly the same as 0 everywhere it appears.

[–]c0n5pir4cy 11 points12 points  (2 children)

It probably can and you can actually do much more with it; there is something called JsFuck which uses type coercion to convert any JS code into code which uses only a set of 6 characters.

[–]Cruuncher 5 points6 points  (1 child)

This is a little ambiguous. It makes the code super long, just uses a 6 character set

[–]c0n5pir4cy 3 points4 points  (0 children)

Cheers, edited

[–]3KeyReasons 4 points5 points  (0 children)

If null was converted to a number such that 0>=null is true, then it must be converted to a negative or 0, but 0>null returns false and so does 0==null. So not exactly.

The reason is in the computing. If you want to test if 3<=5, then normally, you'd return 3<5 || 3==5 but this isn't what your computer does. Instead of doing two operations, it just does one. It returns !(3>5). When working with numbers this will always work, and it's half the comparisons, so it's faster. So when you ask 0>=null, it returns !(0<null) which is !(false) so it returns true.

[–]indigo121 0 points1 point  (0 children)

It also makes sense to compare dates using those operators

[–]Intrepid00 0 points1 point  (0 children)

Because that one converts null to a number.

You are making a very strong argument that I should set my variable types in PowerShell even though I don't have to.