Search code examples
javascriptparseintradix

Using the parseInt() function and the radix parameter with ternary operators


As a case study, I am trying to get to grips with this code snippet that uses the parseInt() function, and have a couple questions:

var maxChars = parseInt( formField.attr('maxlength') ? formField.attr('maxlength') : counter.text() );
  • Why is formField.attr('maxlength') there twice?

  • How does a Radix parameter work in this example?


Solution

  • 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).