Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is it recommended to provide optional radix parameter to parseInt()?

I've always used the parseInt() function in Javascript without passing the radix parameter. As per the MDN documentation here, it's stated that not providing this parameter may result in unpredictable behaviour.

Always specify this parameter to eliminate reader confusion and to guarantee predictable behavior.

Could somebody clarify what does this unpredictable behavior mean with some code examples?

like image 838
Beginner Avatar asked Jul 07 '17 12:07

Beginner


People also ask

What is the purpose of the radix parameter in the parseInt () method?

JavaScript parseInt() Function The radix parameter is used to specify which numeral system to be used, for example, a radix of 16 (hexadecimal) indicates that the number in the string should be parsed from a hexadecimal number to a decimal number.

What will be the radix value of the parseInt () method when the string begins with 0?

What will be the radix value of the parseInt() method when the string begins with 0? Explanation: If the string begins with 0x, then the radix value will be 16. If the string begins with “0”, the radix is 8 (octal).

Why do we need to parseInt?

The main purpose of using the parseInt function is to extract a number from a string. This turns the returned value to an actual number. In the example above, 3 is a string and not an actual number.

What is second parameter of parseInt?

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.


2 Answers

In older versions of the language, parseInt() would cause the function to obey the normal JavaScript numeric constant syntax rules, including the recognition of a leading zero to denote octal constants, and a leading 0x to denote hex constants. Thus, if your code didn't explicitly insist on base 10, stray (possibly user-supplied) numbers with leading zeros would be interpreted as base-8 values, and leading 0x as hex.

The base-8 behavior is gone since ES5.1 (I think; might have been earlier), but the base 16 behavior is still there. (Probably a leading 0x is a little more rare as an accidental prefix than a simple leading 0.)

My experience looking at code here on Stack Overflow is that parseInt() is overused anyway. It's usually cleaner to convert strings (often, strings taken from DOM element .value properties) to numbers with the unary + operator:

var count = +document.getElementById("count").value;

That won't necessarily give you an integer, of course. However, what it will do is notice that the input string has trailing non-numeric garbage. The parseInt() function will simply stop parsing a string like "123abc" and give you 123 as the numeric value. The leading + will however give you a NaN.

If you need integers, you can always use Math.floor() or Math.round().

edit — a comment notes that ES2015 requires a leading 0o or 0O in "strict" mode for octal literals, but that doesn't apply to parseInt() which (in ES2015) only overrides the default radix for hex strings.

like image 81
Pointy Avatar answered Oct 29 '22 00:10

Pointy


For some reason best known to themselves, the folk specifying the behaviour of this function set the radix to be a defaultable parameter but then decided to leave the default value up to the implementation! (Perhaps it would have been sensible to insist on a value of 10 but maybe that would have upset folk progamming in the 1970s who still consider octal literals to be useful.)

So for robust progamming, you need to supply the radix parameter yourself.

like image 42
Bathsheba Avatar answered Oct 28 '22 23:10

Bathsheba