Hello!

I completed this parseInt lesson and I think I understand it, but reading this wording from the lesson made me think of a couple questions that I haven’t found answers for by googling:

const a = parseInt(“11”, 2);

The radix variable says that `11`

is in the binary system, or base 2. This example converts the string `11`

to an integer `3`

.

I thought integers are whole numbers in any base, which includes ‘11’ in binary. But since the lesson says it converts ‘11’ to an integer, that implies ‘11’ is not an integer in binary. Does that mean that in JavaScript, integers only exist in base 10 ?

Also, what if I wanted to convert from base 2 to another base different than base 10, does parseInt only convert to base 10?

If you do not specify a second argument (the radix), a default value of 10 (base 10) is used for radix.

This is correct.

Integers have nothing to do with the base nor which programming language is used.

1 Like

Thanks for the response! It seems I must have misunderstood the wording from the lesson. Just because parseInt converts (“11”, 2) to the integer 3, doesn’t mean 11 is not an integer in binary.

I probably should have worded my second question better though. For example, with parseInt it seems that I can only convert integers in base 2 through 36 to integers in base 10. What if I wanted to convert an integer from base 10 to base 2? It seems that I cannot use parseInt for this, and I would need to use something else like NumberObject.toString(radix) but this is only for bases 2 through 36 so maybe there is a more comprehensive way.

The wording is misleading. It is already an integer. So, you are really converting it from a base 2 integer to a base 10 integer. Integers (whole numbers) are always integers in any base system (well, excluding weird bases, like base pi). An integer is just a number with nothing to the right of the decimal point (that terminology refers to base 10, really we mean the “fractional part”).

Also, what if I wanted to convert from base 2 to another base different than base 10, does parseInt only convert to base 10?

In JS, a number is just a number. It is stored in the computer as binary and displayed on the screen as decimal. You don’t need to worry about how it is stored. If you are working with binary, octal, decimal, whatever - you just normally store it as a number. Now, displaying it is different. *parseInt* takes a string and converts it to a number. What you would need is a way to convert a number to a string that represents that number with a different radix. The Number object wrapper has a built in method *toString*, that will accept a radix.

So,

```
const foo = 3
console.log(foo.toString(2))
// 11
const bar = 42
console.log(bar.toString(16))
// 2a
```

1 Like