user182944
user182944

Reputation: 8067

parseInt method in JavaScript

I am not able to understand what is the use of second parameter of the parseInt method in JavaScript. Below are some of the outputs:

parseInt("9",10) ====> Output: 9

parseInt("9",100) ====> Output: NaN

parseInt("90",10) ====> Output: 9

parseInt("90",100) ====> Output: NaN

Kindly explain what is the use of the second parameter.

Upvotes: 1

Views: 2252

Answers (6)

CALL ME TZ
CALL ME TZ

Reputation: 229

first, some of your example is incorrect. parseInt("90",10) should be output 90.

the second parameter is ary. parseInt("90",10) means parse "90" as decimal.

but why parseInt(xxx, 100) output NaN? You should try parseInt("90", 36) and parseInt("90", 37).

Understand? there are only 36 letter so that no more letter to display the number witch in 37 hex.

finally, sorry about my english.

Upvotes: 1

Ja͢ck
Ja͢ck

Reputation: 173542

The radix is used to specify the range of allowed numerals, also known as the number system or base; when omitted, it guesses the value by looking at the prefix of your string, e.g. a "0x" prefix means radix of 16 (hexadecimal). It's a good practice to always set this explicitly to 10 for the decimal system.

When a numeral is outside of the specified radix, parseInt() will return NaN.

As for the NaN result you're seeing with a radix of 100, though not documented per se, the allowed range of the radix is [1, 36], i.e. the largest base comprises digits and alphabets.

Upvotes: 2

inliner49er
inliner49er

Reputation: 1450

The 2nd parm is the radix (or base number). The defaut is base 10, so if you omit it, or use 10, then you'll get the same decimal number that you're parsing. If you specify base 16 (hex), then the parsed number is calculated thusly:

Examples:

parseInt("1",16) = 1
parseInt("2",16) = 2
parseInt("3",16) = 3
...
parseInt("9",16) = 9
parseInt("A",16) = 10
parseInt("B",16) = 11
...
parseInt("F",16) = 15
parseInt("10",16) = 16 (1x16 + 0)
parseInt("11",16) = 17 (1x16 + 1)
parseInt("12",16) = 18 (1x16 + 2)

Does that help?

Upvotes: 1

josephtikva1
josephtikva1

Reputation: 789

The second paramater is known as the Radix -- see here http://en.wikipedia.org/wiki/Radix, which is used to specify the numbering system. See here for a more detailed explanation http://mir.aculo.us/2010/05/12/adventures-in-javascript-number-parsing/

Upvotes: 3

Parakoopa
Parakoopa

Reputation: 51

Quoting the MDN (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt):

radix
An integer that represents the radix of the above mentioned string. Always specify this parameter to eliminate reader confusion and to guarantee predictable behavior. Different implementations produce different results when a radix is not specified.

Think of it like the base of the number you try to parse, it will return the number in base 10:

parseInt(11,10) // -> 11
parseInt(11,2) //-> 3 (binary)
parseInt(1,6) // 1
parseInt(2,6) // 2
parseInt(3,6) // 3
parseInt(4,6) // 4
parseInt(5,6) // 5
parseInt(6,6) // NaN
parseInt(7,6) // NaN
parseInt(8,6) // NaN
parseInt(9,6) // NaN
parseInt(10,6) // 6, since 5 is the highest digit in base 6 and because of this after 5 comes 10

Upvotes: 1

Durandal
Durandal

Reputation: 5663

It's the base. So if it's 10, it operates as normally.

parseInt("9",8) ===> NaN
parseInt("7",8) ===> 7

Docs are here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt

Upvotes: 2

Related Questions