Hacker News new | ask | show | jobs
by angli 5256 days ago
This is incredibly funny, but does anyone know why the interpreter makes such bizarre decisions? Why wouldn't it be preferable to just throw up errors?
5 comments

JavaScript has a strong design principle of not throwing errors for syntactically valid constructs. The most common way it accomplishes this is through gratuitous use of implicit type conversions. It does a lot of implicit string conversions, even on things that you would think of as error codes, like undefined or NaN.

JavaScript went down the path of being a "forgiving" language because it was intended to provide "extra" (non-core) functionality, under a lot of environments, in the hands of non-experts. It made sense at the time that it should fail silently rather than noisily to not crash the page it was on. It also tried to help amateurs who just banged on code until it did what they wanted, by generally doing something rather than nothing, and by trying to infer intended behavior from undisciplined code. It was never intended to make large-scale or robust applications, so it didn't make decisions that would facilitate that use-case.

I wish there was some technology that could take a bunch of code and warn you of common errors before having it execute and 'noisily crash the page it was on'.

Maybe someday...

Sarcasm doesn't work here. It's not reasonable to expect proper testing or usage of an IDE by ordinary citizens authoring HTML. Specially in 1996.
Yeah, I mean, it's not like they had compilers back then. Savages.
The Ruby stuff apparently isn't bizarre. From what I've read, Ruby creates variables as it encounters an assignment to them when parsing the code. So, when you have

   a = a
in your code, Ruby creates the variable a before it ever tries to actually execute the assignment. If later, when it has finished parsing everything, and starts executing, that statement fails because b is not defined, you still end up with variable a being defined, and since it has not had anything successfully assigned to it, it has a value of nil.

The JavaScript stuff, I think, comes from operator overloading. The plus operator is overloaded to allow adding strings to concatenate them, and it will do type conversion to get compatible types, so "wat"+1 results in the 1 being converted to a string, and then the strings are concatenated. Since the minus operator is not so overloaded, "wat"-1 instead is treated as numerical subtraction. JavaScript allows string to be used as numbers, so "123"-1 gives 122. However "wat" is not a string that represents a number, so gives NaN when forced to be treated as a number, and "wat"-1 is thus NaN.

Thanks for that. I was curious the reason, it seems obvious though after an explanation.
Javascript was a rush job. And the ECMA spec was needed early due to industry pressure. So where perl and (to a lesser extent) python were able to just bury their early goofs in a pile of incompatible versions bumps, Javascript had its encased in the proverbial carbonite of an international standard. Which is sad, really, because in a lot of other ways it's the cleanest scripting language of its generation.
Welcome to the world of JavaScript. This response is NaN.
For more sadness, see: http://wtfjs.com

Fun fact: the creator of Javascript posts on HN. Maybe he'll answer your question.