Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community!

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript JavaScript Foundations Numbers Parsing Numbers From Strings

Chris Mitchell
Chris Mitchell
12,719 Points

Strange behaviour

var u = parseInt("019");

This should return a result of 1 according to the video but when i use this code in sublime text and view in chrome browser i get the value 19 without having to add the base value inside the code var u = parseInt("019, 10");

Is this just an improvement of the browsers or something????

2 Answers

Hi Chris,

It can either be interpreted as an octal or decimal number if the radix is left off. It looks like Chrome is interpreting it as decimal.

This is from the "description" section on the following mdn page for parseInt():

  • If the input string begins with "0", radix is eight (octal) or 10 (decimal). Exactly which radix is chosen is implementation-dependent. ECMAScript 5 specifies that 10 (decimal) is used, but not all browsers support this yet. For this reason always specify a radix when using parseInt.

So it looks like if the browser has implemented ECMAScript 5 then it will be interpreted as a decimal number. Perhaps older versions interpreted it as octal.

I just checked in firefox and I also get 19. For this reason, it's probably best to follow their recommendation to always specify a radix.

Chris Mitchell
Chris Mitchell
12,719 Points

yes, i should really finish off watching the whole video before posting lol, the tutor does indeed explain this towards the end but ty for your help

You're welcome.

I guess I should have reviewed the video myself since I couldn't remember if that was covered or not.