35 points · by gruseom · 18y ago · 20 comments
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.
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.
null == 0 -> false?
It can't be because operands are converted to numbers or booleans, since 0 == 0 and false == false are both true. js> r = RegExp("asdf", "g");
/asdf/g
js> r.test("asdf")
true
js> r.test("asdf")
false
js> r.test("asdf")
true
js> r.test("asdf")
false
Surprisingly this is not a bug, though I was certain it was at first. When using the global flag the RegExp object will keep track of the last matching character position and start the next comparison at that position (after the 4th character, in this case) which results in this seemingly incorrect alternation between "asdf" matching "asdf". Removing the global flag produces correct results.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:
>>> -1 % 3
2
In Javascript: >>> -1 % 3
-1No, 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:
>>> a = -1
>>> b = 3
>>> (a/b)*b
-3
>>> (a%b)
2
>>> (a/b)*b + (a%b)
-1
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 () {
return
{
value : 10
};
}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.