Wednesday, August 8, 2012

ECMAScript parseInt

So I seem to have a love hate relationship with ECMAScript. Today is very much not a loving day for ECMAScript.

I needed to convert text like '06' to an integar, so I just used the parseInt function.

What I did not reliase was that if you do not provide a value for the radix you get very strange results like so:

 js> print(parseInt('06'));   
 6  
 js> print(parseInt('08'));  
 NaN  
 js> print(parseInt('08',10));  
 8  

When you add the radix everthting works as expected as can be seen above.
Note to self: Don't assume functions work the same across all programming languages.

Really enjoyed this video: WAT -> https://www.destroyallsoftware.com/talks/wat

This was a nice 'bug' I found using ECMAScript

js> var a = '1';
js> var b = a + 1;
js> print(b);
11
WAT!

js> var a = 1;
js> var b = a + 1;
js> print(b);
2
 

No comments:

Followers