convert a number from base 2 to base 10 in javascript

To convert a number from base 2 to base 10 in javascript, we can use the parseInt() function:

index.tsx
let num = "1101"; // the binary number
let base = 2; // the base we're converting from
let decimal = parseInt(num, base); // the decimal equivalent
console.log(decimal); // output: 13
184 chars
5 lines

In this example, we pass the binary number 1101 and the base 2 as arguments to the parseInt() function, which returns the decimal equivalent 13.

gistlibby LogSnag