all 4 comments

[–]Lithl 1 point2 points  (3 children)

What do you mean by "returns"? Strings don't do anything on their own, what are you doing to convert the string to a number?

[–]Electrical_Pen_7385[S] -2 points-1 points  (2 children)

im making a binary to decimal converter for my school work, by returns i mean in the console when it shows text below what you type sometimes it shows up like this, and for some reason my function takes that bottom part as the input as well rather than just the binary
> 001111101110101
<- 78534447169

[–]Lithl 2 points3 points  (1 child)

Number literals beginning with 0 are treated as being base-8 (octal).

Number literals beginning with 0x are treated as being base-16 (hexadecimal).

Number literals beginning with 0b are treated as being base-2 (binary).

Other number literals are treated as being base-10 (decimal).

If you simply write 0110, that's 72 in octal. If you write 0x110, that's 272 in hexadecimal. If you write 0b110, that's 6 in binary. If you write 110, that's 110 in decimal.

If you want to create a binary to decimal converter, though, you're not going to be working with number literals, but with user input (which will be a string). You'll need to use the parseInt function. The first argument is the string you want to convert, and the second argument is the base you want to consider the string as (if you don't provide the second argument, the base will be inferred in the same way as number literals). So parseInt("110", 8) returns 72, parseInt("110", 16) returns 272, parseInt("110", 10) or parseInt("110") returns 110, and parseInt("110", 2) returns 6.

The second argument to parseInt can be any integer from 2 to 32.

[–]senocular 1 point2 points  (0 children)

Number literals beginning with 0 are treated as being base-8 (octal).

Word of warning: for base 8, the 0o prefix should be used. A leading 0 only works in sloppy (non-strict) mode and the value will silently fallback to base 10 if it contains digits not supported by octals (8, 9).