# How to convert binary to decimal in JavaScript easily

Converting a number from binary to decimal is a mathematical approach. We can find several mathematical algorithms to find out the decimal value of a binary number and vice versa. But implementation of those algorithms in coding just to convert a binary number to decimal number of convert a decimal number is just ridiculous.

I love JavaScript because like other popular languages JavaScript also have a ready-made inbuilt function which is able to convert strings to numbers. This method will be very useful for us to convert a binary number to decimal.

## Convert Binary to Decimal In JavaScript

Convert a binary number to a decimal number using JavaScript will be easiest for us if we use

`parseInt();`

This method can take two parameters.** In the first parameter**, we can pass any string. So we can pass number string and this method will convert the string to a number.

Just like this one:

If we pass “1457” this method will convert it into a number like 1457.

Now,** In the second parameter, **we can pass the base. That means in which number system the string is. eg, decimal, binary, hexadecimal etc.

Now you can easily predict what I am going to do to convert a decimal number to a binary number.

The default number system in which parseInt() method returns its value is decimal.

var binary = "101101"; var decimal = parseInt(binary, 2); alert(decimal);

This will give you the output in an alert box.

The full example in HTML code,

<!DOCTYPE html> <html> <head> <title></title> </head> <body> <script type="text/javascript"> var binary = "101101"; var decimal = parseInt(binary, 2); alert(decimal); </script> </body> </html>

You may also read,

Guess The Number Game Using JavaScript

How to sort array of numbers in JavaScript Easily

## Leave a Reply