JavaScript(JS) JS convert decimal to binary

https://‮figi.www‬tidea.com

In JavaScript, you can convert a decimal number to binary using the toString() method with a radix of 2. Here's an example:

const decimalNumber = 123;

const binaryString = decimalNumber.toString(2);

console.log(binaryString); // "1111011"

In the above example, we define a decimal number 123. We then use the toString() method with a radix of 2 to convert the decimal number to a binary string. The resulting binary string is "1111011".

Note that the toString() method returns a string representation of the binary number, not a binary data type. If you need to work with the binary data type, you can use the parseInt() method with a radix of 2 to convert the binary string back to a decimal number. Here's an example:

const binaryString = "1111011";

const decimalNumber = parseInt(binaryString, 2);

console.log(decimalNumber); // 123

In this example, we define a binary string "1111011". We then use the parseInt() method with a radix of 2 to convert the binary string to a decimal number. The resulting decimal number is 123.