binary to decimal js code example

Example 1: javascript convert string to 2 decimal

var twoPlacedFloat = parseFloat(yourString).toFixed(2)

Example 2: convert decimal to binary javascript

var n = 5;
n.toString(2); // "101"

Example 3: convert binary to decimal javascript

var digit = parseInt(binary, 2);

Example 4: javascript convert to and from binary

/* From decimal to binary (similar fo rother base) */
const num = 7;
num.toString(2); // output: "111"

/* From binary (similar fo rother base) to decimal */
parseInt("111", 2); // output: 7

Example 5: binary to int javascript

let binary = 0001
parseInt(binary, 2)
// returns 1

Example 6: 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]))