DEV Community

Discussion on: Explain JS's radix in parseInt(str, radix), like I'm five.

Collapse
 
wlindley profile image
Walker Lindley

There are different ways to represent the same number depending on which system you're using. You'll often here these systems referred to by the number of character in them. For instance, base 10 uses ten characters to represent numbers whereas base 2 (also called binary) uses two characters (0 and 1). Other common systems are base 8 (octal), base 16 (hexadecimal), and base 64. By way of example, the base 10 value 2 would be represented as 10 in base 2. Another word for "base" in this context is "radix". So radix is the number of characters used to represent values in a particular number system.

Alright, so what about parseInt? Well it converts strings to integers as you've probably already guessed. Usually people write numbers in base 10, so parseInt assumes you want to use base 10 if you don't specify something else. But sometimes you'll be calling parseInt on strings in one of these other systems such as binary or hexadecimal. In those cases, parseInt makes it easier to convert those values to integers by letting you specify whatever radix (aka base) you want.