

Why does parseInt(1/0, 19) return 18? - onemach
http://stackoverflow.com/questions/11340673/why-does-parseint1-0-19-return-18

======
mattdeboard
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.

~~~
lucian1900
I would say the (bigger) problem is in JavaScript randomly calling toString on
things, especially when you're calling parseInt on a number already.

~~~
VMG
Generally type coercion seems like a bad idea in most languages

~~~
chris_wot
Which is why you can use === to bypass type coercion when dong an equality
test.

~~~
lucian1900
But === is the _other_ equals, not _the_ equals.

Also, that only helps in boolean contexts, not with any function calls.

~~~
chris_wot
_Also, that only helps in boolean contexts, not with any function calls._

Could you clarify?

~~~
lucian1900
'1' === 1 is false, because === is strict.

But there's no strict equivalent of + or -, so '1' + 1 is '11', but '1' - 1 is
0.

The same holds for parseInt('Ambulance', 16) and parseInt('A', 16), which are
both 10.

------
MrEnigma
If this was in PHP people would be crying out what a bad and horrible language
it is.

Javascript it's seen as an amusing conundrum.

~~~
asto
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!

~~~
maggit
Sorry, your point about jQuery is off the mark. jQuery is a library (not a
framework), and if you are not using libraries you are most likely doing it
wrong, in any language!

jQuery does nothing to alter the language, so people using it are still coding
pure JavaScript. jQuery itself is implemented in pure JavaScript.

\----

There does exist variants of JavaScript, such as Paper.js, Protovis and maybe
even CoffeScript, but these are not what the majority of JavaScript users use.

~~~
DrJokepu
I'm not so sure whether jQuery can be considered a framework or not. If you're
using jQuery properly ($(document.ready).ready() and so forth) jQuery behaves
very much like a framework, with inversion of control and other patterns.

JavaScript in the browser has access to the standard JavaScript and DOM
libraries. I really don't think that not using any third-party libraries in
any language means "you're doing it wrong". Pure JavaScript/DOM can go a very
long way (especially in these days of more standard-compliant browsers), just
like pure Objective-C & Cocoa or pure C# & .NET.

Finally, while obviously jQuery does not alter the language, it really changed
how people approach problems in JavaScript. Code that uses jQuery looks
nothing like code that doesn't use it.

~~~
maggit
jQuery is big and important and has lots of features that you can use or
decide not to use. However, if you decide to implement simple animations
yourself in a situation where jQuery would have been a fit, you are likely
wasting time and effort.

Using jQuery for this does not mean that you like or dislike the language.
That was the point I was trying to refute.

Animations is of course also not the only problem you can solve by using
jQuery as a library every now and then.

\----

For what it's worth, I don't think jQuery qualifies as a framework in the
definition on Wikipedia ( <http://en.wikipedia.org/wiki/Software_framework> ),
but this is besides the point.

------
LBarret
Please put Python or Ruby or X in the browser.

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.

~~~
saraid216
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.

~~~
chris_wot
Nobody would use it as it wouldn't have market share. But that's besides the
point. The behaviour is actually well defined; the only issue is with
parseInt() taking the first character it sees as a number, converting it from
it's base to base 10 and then prints the number. The issue being, of course,
is that it would have been more sensible to given an exception for non-numeric
strings.

~~~
saraid216
> Nobody would use it as it wouldn't have market share.

That's why I added the part about forking and contributing back. If it becomes
part of the standard offering, then you don't have to worry about market share
as much.

And especially if you get a portable implementation then the IE and Chrome
teams might decide to adapt it in, since you've lowered the engineering cost.

> But that's besides the point. The behaviour is actually well defined

There's a good reason to _not_ use Javascript, if there's an option to use
another language in the browser, that has nothing to do with JS's quality
itself: context switching. Node.js is popular due in no small part to the fact
that you have to use JS anyways. The purpose of GWT, as far as I'm aware, is
to make it so you have Java as an option in the client.

------
chris_wot
Ah! Nice work - that makes total sense.

Incidentally, PHP doesn't have the concept of positive or negative Infinity,
just NaN. However, when I tried:

    
    
      print inval(1/0);
    

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
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)

~~~
chris_wot
Argh! Sorry, proxy error caused all sorts of problems :(

------
jaimebuelta
Am I the only one that has laugh maniacally after reading this? X-D

