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

3

35

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?

Inappreciative answered 1/5, 2012 at 14:5 Comment(1)
There is no radix specified in this example... The entire expression inside the parseInt() is a ternary statement which returns the maxlength if it is specified or counter.text() if it isn't.Zephyrus
I
49

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

Irradiation answered 1/5, 2012 at 14:6 Comment(3)
Firefox, Chrome and IE 9+ treat parseInt("077") as parseInt("077", 10). IE8 and below treats it as parseInt("077", 8) That's why you should always specify the radix.Assuasive
@JuanMendes I believe that ES5 deprecated the original behaviour of defaulting to octal for a leading zero.Irradiation
@Irradiation That is correct ecma-international.org/ecma-262/5.1/#sec-15.1.2.2 I still would always use it for clarity (even if you don't care about older browsers) since 077 === 63Assuasive
L
4

In the ECMA Script 5 when the string starts with 0 and no radix is specified the default behavior is decimal (as opposed to the earlier versions in which it was octal)

Source: parseInt() on Mozilla Developer Network

Lacey answered 4/6, 2014 at 19:48 Comment(0)
C
-1

parseInt takes two parameters, the second one is optional. String and Radix.

String is the value to parse. If the value provided is not a string it will convert it to a string.

Radix is an integer between 2 and 36 that represents the radix (the base in mathematical numeral systems) of the above mentioned string.

In your code snippet the Radix isn't specified and is assumed to be default 16.

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

You are defining a variable called "maxChars". This variable is equal to the evaluation of a short hand IF statement.

You are getting the attribute from the variable which is expected to be a selector "formField" called "maxLength". The value will return as a integer, it will fallback on it's default radix.

The IF statement checks if the returned value is true or false. 0, false, ectcetera would result in the value of the variable "maxChars" to be set to "counters" combined text. IF true it would result in the variable to be set as selector "formField" attribute called "maxLength".

formField.attr('maxlength') is there twice because one is used in an IF statement evaluation and the other is used as the value if the condition in the IF statement results as TRUE.

Constellate answered 2/8, 2015 at 18:9 Comment(1)
a bit late, but Radix isn't specified but it doesn't always default 16 and this sentence is wrong because of that second half. it depends on the browser and the actual string being passed in. And also since ES5 (as stated in the comments in the accepted answer) usually defaults to decimal/10Chalky

© 2022 - 2024 — McMap. All rights reserved.