Live data from Hacker News

JavaScript: The Curious Case of null >= 0

blog.campvanilla.com

151–152 of 152 posts

Re: JavaScript: The Curious Case of null >= 0

#151

Not to defend JavaScript too much, but I think the three examples seem reasonable when viewed in the right way, and I think it's hard for JavaScript to do much better with its existing design philosophy (dynamic typing and coercion rather than runtime errors to deal with type mismatches). >= and > are numerical operations, so both sides are interpreted as a number, and null is 0 when treated as a number. That means `…

Let me agree with you but then turn that on its head. You've argued that having `null >= 0` is the best that JS could do while having a coercion-based semantics. And I agree: this confusing behavior may be the best you can possibly do with a coercion-based semantics. And it's not just this example, either. JavaScript is filled with gotchas, and a lot of them have to do with automatic coercion. For example, did you kn…

It took me a bit to dig through the fringe cases to find the combination (or maybe an additional combination?) to pull that off.

x = ''

y = '-Infinity'

z = -1

x y z Thank you for the brain exercise!

P.S. - No one ever use crap like this in production code!

Re: JavaScript: The Curious Case of null >= 0

#152
post #151

Earlier quoted context omitted.

Let me agree with you but then turn that on its head. You've argued that having `null >= 0` is the best that JS could do while having a coercion-based semantics. And I agree: this confusing behavior may be the best you can possibly do with a coercion-based semantics. And it's not just this example, either. JavaScript is filled with gotchas, and a lot of them have to do with automatic coercion. For example, did you kn…

It took me a bit to dig through the fringe cases to find the combination (or maybe an additional combination?) to pull that off. x = '' y = '-Infinity' z = -1 x y z Thank you for the brain exercise! P.S. - No one ever use crap like this in production code!

Oh interesting. The triple I found used the same general idea of values that sometimes coerce to a number, but it made use of octal values instead of Infinity.
Post reply on HN