The blog has moved!
If you are interested in reading new posts, the new URL to bookmark is http://blog.valeriogheri.com/
I know a good developer should always read the manual, but sometimes somebody is just brave enough to start coding withouth caring to read it all… and that’s how I ended up having my “WAT?” moment using the function parseInt().
I was trying to create a Date object from a json serialized data sent by the server, using parseInt() to transform the day, the month and the year into an int. For several data points the process was successfull, whereas for just a few it was not and I could not understand why… until I pointed Chrome debugger to the right line of code and found out that parseInt has a funny behaviour if we do not specify the radix.
Let me show you what I mean: if you type each of the following line into Chrome developer console, the result you’ll get is what comes after the “–>”
If the input
stringbegins with “0”, radix is eight (octal). This feature is non-standard, and some implementations deliberately do not support it (instead using the radix 10). For this reason always specify a radix when using