Javascript null surprise
I just encountered this while tracking down a bug in some Javascript (verified in FF and IE):
null < 0 || null == 0 -> false
but null <= 0 -> true
Would you expect that? I didn't. Obviously <= is implemented as !>. Time to look up all uses of <= and >= in my code...p.s. Is this too trivial to post here? Too specialized? I'm curious.
(
20 comments
| 35 points
| on HN
)
If it's given two objects of different types, then it has to coerce one of them. In this case, I would expect it to coerce null to 0, then compare 0==0.
Your way would have it coerce 0 to false and null to false, so it's still equal, but it has to coerce two things.
I can sort of see the logic in this though, since a lot of other languages (SQL) have the rule that nothing is allowed to be equal to null.
Makes me wonder what the rules are for coercion though, that is does it pick null to coerce or 0?
null==0 || 0==null -> false
It's weird for number/string comparisons, too. "12" is greater than "102" (lexical ordering, "2" > "0") but less than 102 (coerces "12" to number).
It seems like if one side is a number, then it coerces the other one to number as well, unless the other is null.
Which makes sense given what it was designed for, since any attribute you pull in from a DOM element will be a string, and you want to make forgetting to convert safe.
You can use the following javascript link to confirm this:
(Note - I can't ever remember how to embed links properly in YC... is there a reference somewhere?)
javascript:alert('null < 0: '+(null< 0)+'\nnull ==0: '+(null==0)+'\nnull > 0: '+(null>0)+'\nnull<=0:' + (null<=0) + '\nnull < null: '+(null<null)+'\nnull == null: '+(null==null) + '\nnull > null: '+(null>null)+'\nnull<=null: ' + (null<=null));
Both Safari 3 and Firefox 1.5 do the decidedly unexpected null <= 0 thing.
In Python:
In Javascript:No, python is giving you the Modulus after division (i.e. a "distance" which should be positive). If you interpret it that way everything is fine:
Perhaps you're thinking of the Remainder?Edit: it seems there isn't much consensus about what to do when a or b are negative. Wikipedia gives a helpful chart here how different languages choose to implement this: http://en.wikipedia.org/wiki/Modulo_operation
>>> 1 % -3 -2 >>> -1 % -3 -1
function hello () {
}window.alert (hello ().value);
It burps in FF and IE...
This is actually a result of JavaScript's lax support of semi-colons. Semi-colons are generally not enforced in JavaScript, but this leads to some ambiguous cases. Your case may not seem ambiguous, but the following code snippet will show what is actually going on:
if (i==5) return
i += 3;
As you can see, there are 2 possible meanings for this when there is no explicit semi-colon after the return. It can either mean "if i equals 5 return, else add 3 to i" or it could mean, "if i equals 5 return i plus 3". JavaScript does not try to discern which is "more correct" (since there are no return types on functions so this would be a very difficult task), but rather defaults to "floating returns" meaning "return;".
I understand what you mean, in this case though it would be helpful if the interpreter detects the statement as being not reachable since it's after the return.