JavaScript: parseInt()

parseInt()

parseInt()
parseInt(numStr, radix)
2 parameters:
  • numStr: The number in the radix system
  • radix (optional): The radix/base
Return: the radix-10 value (type number), including NaN
Explanations:
  • numStr: The number in the radix system
    • Valid numbers:
      • Regular numbers (type number or a string)
      • Octal (0 leading), hex (0x leading), and is there more? (type number)
      • Alphanumeric numbers (type string)
    • Any strings are actually operable (even symbols):
      • String is processed digit by digit from the start to the next. Conversion per digit are assembled back by concatenation, then returned as number.
      • For the conversion to happen, the digit must be a valid number of the radix.
        Non-valid digit includes:
        • alphanumeric larger than the radix
        • symbols
      • When it encounters non-valid digit, the operation is stopped, and the rest of the digits are not even visited.
        It returns whatever collected already:
        • If it happens in the first digit: return NaN, since there's nothing in the reservoir.
        • If it happens later: return whatever value have been concatenated from previous digits.
      • Note that either way it returns a number data.
  • radix (optional): The radix/base
    • Valid radix: 2-36
    • Considered base 10:
      • 0
      • Non number string: Alphabet, alphanumeric, symbols, or any combinations
      • Octal as string. (But hex as string will be considered hex. See explanation on type below).
      • Second argument not given
    • Any other numbers: Return NaN
      • -1 and under
      • 1
      • 37 and above
    • Decimals: accepted. They are floored or rounded to 0 for 0's negative decimal.
    • Type:
      • Type number: accepted, including octal, hex numbers (don't quote)
      • Type string: accepted. Will first tried to be converted to number. But... Careful with string to number conversion rule. Octal and hex are best given as number type. Octal's leading zero will be removed and now considered base ten number. While hex's leading 0x will stay, and considered as hex.

References