Converting (binary,hex,octal) to decimal numbers in JavaScript
In this tutorial, we are going to learn about how to convert binary, hexadecimal and octal to decimal numbers in JavaScript.
parseInt
In JavaScript, we have the parseInt() method by using that we can make conversions.
The parseInt( ) method takes the two arguments, the first argument is string and the second argument is base by using that it returns the decimal number of a specified base.
Let’s see the conversions with the help of examples.
Converting binary to decimal
To convert a binary to decimal we need to pass base 2 as a second argument to the parseInt method.
const binaryNumber = "1100";
console.log(parseInt(binaryNumber,2)); // base 2
//output--> 12Converting hexadecimal to decimal
To convert a hexadecimal to decimal we need to pass base 16 as a second argument to the parseInt method.
const hex = "c";
console.log(parseInt(hex,16)); //hex base-16
//output --> 12Converting octal to decimal
To convert a octal to decimal we need to pass base 8 as a second argument to the parseInt method.
const octal = "14";
console.log(parseInt(octal,8)); //octal base-8
//output --> 12You can also learn, how to convert decimal to (binary, hexadecimal, octal) numbers in JavaScript.


