parseInt() and the radix half of the function

Hello,

I am super confused about how the parseInt() function works. I get the easy part in that if you pass it a string it will return a number. That portion is very straightforward. For example:

parseInt(“007”) // would return 7

Where I am super lost on this is when you start incorporating the radix into the function. In the example they had the following:

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

Why would this above example return 3?

I was also trying to reference some other resources for this and got even more lost. The following are examples from Mozilla. Why would all the following examples return 15?

parseInt(‘0xF’, 16)
parseInt(‘F’, 16)
parseInt(‘17’, 8)
parseInt(021, 8)
parseInt(‘015’, 10) // but parseInt(015, 8) will return 13
parseInt(15.99, 10)
parseInt(‘15,123’, 10)
parseInt(‘FXX123’, 16)
parseInt(‘1111’, 2)
parseInt(‘15 * 3’, 10)
parseInt(‘15e2’, 10)
parseInt(‘15px’, 10)
parseInt(‘12’, 13)

Hi @jwink08 !

Welcome to the forum!

The optional radix represents a number base system.

The 2 is for binary. Binary is a number systems made up of 0’s and 1’s.

It is a little confusing at first but maybe a visual will help.
In order to convert binary to a decimal you first have to look at the place values.
Reading from right to left you have the 1’s place, then 2’s place, then 4’s place, etc.

128  64	32	16	8	4	2	1

If we place the binary number of 11 underneath those place values, then we can do the math to see why it returns 3.

128  64	32	16	8	4	2	1
                        1   1

If we add the place values of 2+1 then we get 3.
Hopefully that wasn’t confusing.
But if it was that’ s ok.
My background is not in computer science or math so someone else can probably explain it much better than I can :grin:

I also found this helpful video you can watch

All of those examples from MDN deal with different base systems.
16 would be hexadecimal.
8 would be octal
10 would be decimal
13 would be tridecimal

Here is a good video on Hexadecimals you can watch

I wouldn’t stress to much on parseInt and the optional radix.
Just do the best you can.

But if you are still curious there are a lot fo good videos on each of those systems online that will go more in depth than me. :grinning:

Hope that helps!

3 Likes

An integer is a number without a fractional component.

That means if you add 1 to 0 n times, n is an integer.

parseInt is a function that takes a string and tries to parse an integer from it. So for example:

parseInt("10")

Will return the number 10.

However, numbers can be represented using different syntaxes. They can be written in different ways. Same as human languages: blau, bleu, azul, синий and 蓝色 are all the same as blue, they are just written and pronounced differently.

For numbers, different syntaxes have a different number of numerals available.

Decimal has 10 numerals (0,1,2,3,4,5,6,7,8,9).

Binary has 2 numerals (0,1).

Octal has 8 numerals (0,1,2,3,4,5,6,7).

Hex has 16 numerals (0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f).

So this is the number 10 written in decimal:

10

This is the number 10 written in binary:

1010

This is the number 10 written in octal:

12

This is the number 10 written in hex:

a

parseInt assumes the string contains a decimal. So you don’t need to tell it to look for a decimal:

parseInt("10")

This returns the number 10.

But if the string doesn’t contain a decimal, you need a way to tell the function that. This is what the second parameter (the radix) is for.

So if I had a number represented in binary, but I just did this:

parseInt("1010")

It would return the number 1010.

But if tell the function that it’s binary:

parseInt("1010", 2)

It would return the number 10.

1 Like

Thank you @DanCouper this greatly helped!!

1 Like

Thank you @jwilkins.oboe this helped a lot!!

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.