convert binary to integer javascript code example
Example 1: convert binary to decimal javascript
var digit = parseInt(binary, 2);
Example 2: javascript convert string to number or integer
var text = '42px';
var integer = parseInt(text, 10);
Example 3: javascript convert to and from binary
const num = 7;
num.toString(2);
parseInt("111", 2);
Example 4: binary to int javascript
let binary = 0001
parseInt(binary, 2)
Example 5: convert binary to decimal javascript
const binaryArrayToNumber = arr => {
let len = arr.length
let pow = []
let decimal = []
for(let i = 0; i <= len - 1; i++){
pow.unshift(i)
}
arr.forEach((x,index) => {
decimal.push(x*2**pow[index])
})
let toDecimal = decimal.reduce((acc, curr) => acc + curr, 0)
return toDecimal
};
console.log(binaryArrayToNumber([1, 0, 1, 1]))