News for hackers. Stuff that matters.
Re:Javascript null surprise by slurpme on Friday February 22, 2008 @06:28PM (#121677)
But isn't the context a Boolean expression, so the 0 would be converted to a Boolean as well?
Re:Javascript null surprise by randrews on Friday February 22, 2008 @06:43PM (#121686)
== needs two arguments of the same type. If they're both strings, it's a string compare. If they're objects, it does whatever it does for objects (identity, I think). If they're numbers, then it's a numeric comparison.

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.

Re:Javascript null surprise by slurpme on Friday February 22, 2008 @06:47PM (#121690)
Yes I think you are right about that, too early in the morning for me...

Makes me wonder what the rules are for coercion though, that is does it pick null to coerce or 0?

Re:Javascript null surprise by randrews on Friday February 22, 2008 @07:12PM (#121703)
I just experimented a bit with Rhino ( http://www.mozilla.org/rhino/ ) and I got nothin'. I had thought it might be that it coerces the right hand side to match the left's type, but it doesn't since:

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.

Re:Javascript null surprise by hhm on Saturday February 23, 2008 @07:22AM (#121991)
Thanks for the insightful reply.