Using Javascript parseInt() and a Radix Parameter

shrewdbeans picture shrewdbeans · May 1, 2012 · Viewed 17.4k times · Source

Can anyone explain how the parseInt() functions works and what the Radix Parameter is?

As a case study, I am trying to get to grips with this code snippet:

var maxChars = parseInt( formField.attr('maxlength') ? formField.attr('maxlength') : counter.text() );

Can you also explain how this code works? Why is formField.attr('maxlength') there twice? I find the use of operators here pretty darn confusing!

How does the Radix Parameter work in this example?

Answer

Alnitak picture Alnitak · May 1, 2012

The radix is another name for base, i.e. 2 for binary, 10 for decimal, 16 for hexadecimal, explained in more detail on the Mozilla Developer Network site.

In your example there is no radix parameter, so the interpreter will fall back to the default behaviour, which typically treats numbers as decimal, unless they start with a zero (octal) or 0x (hexadecimal).