96

What does radix actually means? Why do we need it?

parseInt(10, radixValue); 
Sᴀᴍ Onᴇᴌᴀ
  • 7,491
  • 8
  • 27
  • 56
John Cooper
  • 6,277
  • 26
  • 72
  • 97

9 Answers9

109

You might not always want to parse the integer into a base 10 number, so supplying the radix allows you to specify other number systems.

The radix is the number of values for a single digit. Hexidecimal would be 16. Octal would be 8, Binary would be 2, and so on...

In the parseInt() function, there are several things you can do to hint at the radix without supplying it. These can also work against you if the user is entering a string that matches one of the rules but doesn't expressly mean to. For example:

// Numbers with a leading 0 used a radix of 8 (octal) before ECMAScript 5.
// These days, browsers will treat '0101' as decimal.
var result = parseInt('0101');

// Numbers that start with 0x use a radix of 16 (hexidecimal)
var result = parseInt('0x0101');

// Numbers starting with anything else assumes a radix of 10
var result = parseInt('101');

// Or you can specify the radix, in this case 2 (binary)
var result = parseInt('0101', 2);
Justin Niessner
  • 229,755
  • 35
  • 391
  • 521
  • 2
    So parseInt('0101') gives 101 or 65 ? – Vivekh Aug 23 '15 at 06:48
  • 7
    @Vivekh - It used to back it 2011 when I originally posted the answer. A quick check in Google Chrome seems to indicate that they've changed a bit. `parseInt('0101')` now returns `101`. Different browsers may have different behavior though. Always include the radix for reliable results. – Justin Niessner Aug 24 '15 at 19:50
  • @Vivekh - You can see the most current documentation at https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt – Justin Niessner Aug 24 '15 at 19:51
  • The most recent documentation is that ES 5 does specify that no radix should be treated the same as passing in a 10, but warns you to pass it in anyway for older browser support. – Juan Mendes Jul 07 '17 at 18:39
  • 1
    I've always used `parseInt(x, 0)` for some reason I can't remember right now.. is this equal to `parseInt(x, 10)` I am wondering now... – OZZIE Feb 11 '19 at 15:13
38

Because if you have a string number like 0700 and want the output to be integer 700 you need to inform parseInt() that it is a decimal number rather than octal.

console.log(parseInt("0700"));
// 448

// I really wanted decimal (base 10)
console.log(parseInt("0700", 10));
// 700

// What is this? Binary, Decimal, Octal?
console.log(parseInt("0110"));
// 72

// as binary
console.log(parseInt("0110", 2));
// 6
Note I only answered half your question. See others for good definitions of what a radix actually is.
Sᴀᴍ Onᴇᴌᴀ
  • 7,491
  • 8
  • 27
  • 56
Michael Berkowski
  • 253,311
  • 39
  • 421
  • 371
  • 1
    Also, binary parseInt('10101', 2) – Joe Jul 07 '11 at 14:03
  • 3
    console.log(parseInt("0700")); is returning //700 not 448. – Naren Oct 14 '19 at 19:25
  • @Naren 8 years ago it was 448 in whatever browser console I ran this in. In modern Chrome and in Node, I also got 700 today, but maybe it was undefined behavior at the time. – Michael Berkowski Oct 14 '19 at 19:40
  • @MichaelBerkowski I was noticing that difference lately- apparently [ES6 octal numbers start with `0o` instead of `0`](https://www.javascripttutorial.net/es6/octal-and-binary-literals/) – Sᴀᴍ Onᴇᴌᴀ Oct 23 '20 at 19:08
8

Radix is the base of a system of numeration. There are an infinite number of numeric systems but the ones with which most people are familiar are base 10 (decimal) and base 2 (binary).

Numeric values can be interpreted differently in different bases. For example, the number 10 in binary can be represented as 2 in decimal.

In the case of parseInt(), the radix allows you to specify the base to be used. By default, a radix of 10 is used.

However, the radix should always be specified, even when using base 10. Consider the case of

parseInt("010") // Returns 8

At first glance, you may expect the statement to return 10. Explicit use of the radix will help to avoid confusion:

parseInt("010", 10) // Returns: 10

George Cummins
  • 26,731
  • 6
  • 65
  • 89
4

The radix is the base number of the number system: http://en.wikipedia.org/wiki/Radix

Normally, you only need to specify the radix if you want it to be different from 10. More specifically (from http://www.w3schools.com/jsref/jsref_parseInt.asp) :

If the radix parameter is omitted, JavaScript assumes the following:

If the string begins with "0x", the radix is 16 (hexadecimal) If the string begins with "0", the radix is 8 (octal). This feature is deprecated If the string begins with any other value, the radix is 10 (decimal)

Mathias Schwarz
  • 6,833
  • 18
  • 27
2

Just adding some additional information which clearly answers the question:

If radix is undefined or 0 (or absent), JavaScript assumes the following:

  • [...]
  • If the input string begins with "0", radix is eight (octal) or 10 (decimal). Exactly which radix is chosen is implementation-dependent. ECMAScript 5 specifies that 10 (decimal) is used, but not all browsers support this yet. For this reason always specify a radix when using parseInt.
  • [...]

Source: MDN parseInt()

nmoliveira
  • 1,609
  • 15
  • 14
2

It's just my opinion but idea that "we need to use radix" quickly becomes outdated. The problem really was actual some time ago because people outside IT world usually don't use number notations other than decimal and quite often provide decimal numbers zero-padded like "010". But since ECMAScript 6 octal numbers in JS are prefixed by "0o" and not just "0" as it was in ECMAScript 5 and 3. So if you don't target IE family (that is not rare situation now) you can skip radix safely.

Konstantin Smolyanin
  • 14,955
  • 10
  • 46
  • 42
1

What is radix in paseInt() ?

The parseInt() function parses a string and returns an integer. It takes a second argument for the radix, which specifies the base of the number in the string. The radix can be an integer between 2 and 36.

The function call looks like (Syntax):

parseInt(string, radix);

Some examples to clarify the concept of radix

Example 1:

var a = parseInt("11", 2);

The radix variable says that "11" is in the binary system, or base 2. Therefore, this example converts the string "11" to an integer 3.

Example 2:

var a = parseInt("10011", 16);

Here the radix tells the parseInt() that 10011 is a hexadecimal number and hence in integer, it gets converted to 65553

Basically, long story short, the radix argument tells the parseInt() that the string passed as 1st parameter is of a particular system (binary, hexadecimal etc) and it needs to be converted into an integer as an end product.

mishsx
  • 1,153
  • 4
  • 15
  • 29
1

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.

If the radix parameter is omitted, JavaScript assumes the following:

If the string begins with "0x", the radix is 16 (hexadecimal)

If the string begins with "0", the radix is 8 (octal). This feature is deprecated

If the string begins with any other value, the radix is 10 (decimal)

Source W3Schools

Jeremy Seekamp
  • 2,646
  • 1
  • 21
  • 24
1

I learned the hard way that you always need to provide radix when working with parseInt in 2011. but in modern browsers, it seems that they "fixed" it.

If you run a function on a string starting with 0, it will parse it with radix 10. But if you run it on a number for whatever reason, then it will still use radix 8, as mentioned in MDN.

console.log(parseInt('015'));
// This returns 15

console.log(parseInt(015));
// This returns 13
domaci_a_nas
  • 86
  • 1
  • 6