JavaScript for the Frustrated Programmer

Once again I find myself confronted with the peculiarities of JavaScript's type coercion (as explained in the first chapter of Eloquent JavaScript). I ran into these before and it frustrated me how JavaScript would do conversions that were heavily weighted towards strings:

"5" + 1  // Returns '51' because of string concatenation.
"5" - 1  // Returns 4 because strings cannot be subtracted.

5 + '1'  // Returns '51'
5 - '1'  // Returns 4

I recall this bothering me pretty hard when I tried the learning challenge last time, but I'm older and wiser now (and unemployed). Plenty of people have been able to hold their noses for long enough to be productive with JavaScript. If I'm going to get anywhere with this language I need to learn this skill too.


links

social