Contrary to what some people may think, this is non-obvious & surprising behavior which has nothing to do with JS being a "dynamic language". It requires knowing that the IEEE floating point standard arbitrarily defines 1/0 as positive infinity instead of being (the more widely accepted, obvious and unsurprising) undefined value. I believe JS is a powerful language but calling this reasonable behavior smacks of Stockholm Syndrome.
It is mainly related to the fact that parseInt in JS slightly converts a positive infinity to a string "Infinity". Base 19 does not add anything to the humor. And well, neither does the presence of a positive infinity itself.
So yes, this is nothing to do with JS being a dynamic language (which main feature is not a variable/slot but a value having a type), but it's something to do with it being a weakly typed language (which main feature is an implicit and generally unpredictable type conversion). Of course many popular dynamic languages are still strongly typed in this sense.
that js implements the floating point standard is not js' fault. but that parse("1something unreasonable", 2) == 1 is just wrong. it should generate an exception.
The reason javascript won't get nearly as much ridicule as PHP is because there's no other choice with javascript. Most arguments against PHP are in the form - I'm using <super awesome language> because PHP has .... faults.
Also, the fact that very few people code pure javascript, instead choosing frameworks like jquery, goes to show how much people like it!
First, Javascript isn't nearly as bad as PHP. Second, even the biggest fans of Javascript recognize its problems (the same can't be said of PHP). Third, even the biggest decractors of Javascript recognize its one overwhelming strength: It's the only language to run in the browser.
The only reason something like this makes it to the front page, is because of the popularity of things like node.js right now. This is a bug, not a feature.
Python also has plenty of stupid edge cases while being less functional, flexible but more complex than JavaScript. My theory is that Python gets good press because nobody has to use Python, so the people who don't like it just use a different language instead of complaining. Obviously, this does not hold for JavaScript.
If you're going to complain about language choices, don't force everybody on Python!
I can't comment on Ruby because I have never had to use it.
What would be perfect is Scheme in the browser. And, apparently, we almost had it except marketing or management forced Eich to make it look like Java resulting in JavaScript.
No one is stopping you from rolling your own browser and taking market share away from the current Big Three.
Or from forking Chrome, since it's open source, and then offering your patch back to the central repo. Not sure if Firefox is open source, but could use that instead, too, if it is.
Please read my comment below regarding this behavior in PHP. In addition, I will add that 1/0 here is not returning NaN (if you attempt to print it as a string you won't get "NaN"), and intval itself actually has the same behavior as JavaScript. (edit: Oh, I just realized the other comment I replied to was also you. ;P)
[+] [-] mattdeboard|13 years ago|reply
[+] [-] lifthrasiir|13 years ago|reply
So yes, this is nothing to do with JS being a dynamic language (which main feature is not a variable/slot but a value having a type), but it's something to do with it being a weakly typed language (which main feature is an implicit and generally unpredictable type conversion). Of course many popular dynamic languages are still strongly typed in this sense.
[+] [-] lucian1900|13 years ago|reply
[+] [-] chris_wot|13 years ago|reply
1. http://grouper.ieee.org/groups/754/faq.html#exceptions
[+] [-] ibotty|13 years ago|reply
[+] [-] MrEnigma|13 years ago|reply
Javascript it's seen as an amusing conundrum.
[+] [-] asto|13 years ago|reply
Also, the fact that very few people code pure javascript, instead choosing frameworks like jquery, goes to show how much people like it!
[+] [-] chris_wot|13 years ago|reply
PHP does the following:
Now try: This returns:[+] [-] Lazare|13 years ago|reply
[+] [-] bentlegen|13 years ago|reply
[+] [-] JoeCortopassi|13 years ago|reply
[+] [-] LBarret|13 years ago|reply
Javascript is no better than PHP. It is there - like Php in its time- because we have no other choice.
We're supposed to be professionals and one of our main tool is a joke.
[+] [-] tikhonj|13 years ago|reply
If you're going to complain about language choices, don't force everybody on Python!
I can't comment on Ruby because I have never had to use it.
What would be perfect is Scheme in the browser. And, apparently, we almost had it except marketing or management forced Eich to make it look like Java resulting in JavaScript.
[+] [-] saraid216|13 years ago|reply
Or from forking Chrome, since it's open source, and then offering your patch back to the central repo. Not sure if Firefox is open source, but could use that instead, too, if it is.
[+] [-] chris_wot|13 years ago|reply
Incidentally, PHP doesn't have the concept of positive or negative Infinity, just NaN. However, when I tried:
I got an exception. But point taken :-)Edit: I might not be correct about positive or negative infinity in PHP... apologies if I have at all mislead anyone!
Also: got a proxy error, so this got reposted, then I deleted one of the posts, but the other post got marked deleted. So third post about this!
[+] [-] saurik|13 years ago|reply
[+] [-] jaimebuelta|13 years ago|reply