In the previous article Javascript parseInt Function we discussed about the first argument of parseInt function. Here we will learn about the second optional argument of parseInt function i.e. Radix. The radix parameter determines which numeric system is to be used to convert the passed value.
Generally we use the parseInt function without its optional radix parameter. parseInt function works as the following assumptions to recognize the numeral system:
1. If parameter value starts with “0” then parseInt function assumes it as Octal number system. Default radix value for Octal is 8.
2. If parameter value starts with “0x” then parseInt function assumes it as Hexadecimal numeric system and tries to parse the value into decimal. Default radix value for hexadecimal system is 16.
3. If the parameter value starts with any other digit then parseInt function evaluates the value normally as decimal number system. Default radix value for decimal system is 10.
Javascript parseInt using Radix parameter Syntax
parseInt(string, radix);
Where radix is an optional parameter for parseInt function
Examples of Javascript parseInt Function using radix
document.write(parseInt("15") + "<br />");
document.write(parseInt("15", 10) + "<br />");
document.write(parseInt("07") + "<br />");
document.write(parseInt("17", 8) + "<br />");
document.write(parseInt("0xF") + "<br />");
document.write(parseInt("F", 16) + "<br />");
Note: Above examples show the use of parseInt function using radix parameter along with their equivalents without radix parameter. Output value for each parseInt parsed value is 15
Continue to next tutorial: Javascript Convert Decimal to Hex to learn how to convert a decimal number into its equivalent hexadecimal number.