how to convert binary string to decimal?
The parseInt
function converts strings to numbers, and it takes a second argument specifying the base in which the string representation is:
var digit = parseInt(binary, 2);
See it in action.
ES6 supports binary numeric literals for integers, so if the binary string is immutable, as in the example code in the question, one could just type it in as it is with the prefix 0b
or 0B
:
var binary = 0b1101000; // code for 104
console.log(binary); // prints 104