Javascript is a very unique language, and I think that has become more and more apparent over the years. Crockford said it best - it's the worlds most misunderstood programming language. And yet it has somehow managed to evolve into a sheer necessity of the web. I can blatantly recall my very first programming experience with Javascript, back in 1998. I spent over an hour trying to figure out why my one line of code to get a form element was not working. Then I found out that Javscript was a case-sensitive language, and this really pissed me off. I could use whatever case I wanted in HTML, so why does Javascript have to be any different?! Regardless of your level of experience as a programmer (in general), you should probably expect some introductory hiccups upon first learning Javascript. I believe that such initial frustrations are why the language initially got such a bad rap. To become an expert, you have to be persistent, inquisitive, and down-right stubborn.
The other day, a co-worker of mine was flipping his wig over the parseInt() function. It was not working consistently, which was probably pretty aggravating. Hell, I know it was aggravating. He came to the realization that it was failing everytime the given number had a leading zero. No, this is not a bug. The problem (of course!) was that he was not specifying a radix. Yes, parseInt() has two parameters, not just one. When you do not specify a radix, it makes an educated guess at what it should be, based on a simple evaluation of the input string. W3Schools clearly states…
- If the string begins with "0x", the radix is 16 (hex)
- If the string begins with "0", the radix is 8 (octal)
- Otherwise, the radix is 10 (decimal)
That said, passing a value of "080" without specifiying a radix is always going to return zero because it is not a valid Octal value. Go ahead, see for yourself. The fix is really simple, and becomes painfully obvious once you understand the above. Make sure to always specify a decimal radix, and it will work. People who have no patience for specifications might realize that using parseFloat instead of parseInt is also a potential workaround. But if you are like me, you have to know why something does not work. And this is just one of the few oddball issues you can expect to encounter if you are new to Javascript.
If you have managed to master the Javascript language without having at least one aggravating encounter like this, you must be inhuman!
Yes, Lincoln....my first programming job was actually for American Games in Council Bluffs
Guess What?
There are a few basic guidelines you should be aware of before leaving a comment…
- If you choose to display your email address, it will not be detected by spam bots
- Comments are limited to 3,000 characters; so far you have used none of them
- HTML will be encoded; links and line breaks will be converted automatically
- Comments containing five or more links will be subject to moderation
