How to write a js code to convert binary number into decimal? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How to write a js code to convert binary number into decimal?

21st Sep 2017, 3:39 PM
kamran arshad
kamran arshad - avatar
2 Answers
+ 3
There isn't an equivalent for binary numbers in Javascript. However you can have binary string converts into decimal using parseInt function. console.log(parseInt('1101', 2)); // output decimal 13
21st Sep 2017, 3:55 PM
Calviղ
Calviղ - avatar
+ 2
var bin = prompt('Enter a binary number (only 0 and 1, none prefix)?'); var dec = 0; var i = bin.length; while (i--) { if (bin[i]=='1') dec += Math.pow(2,(bin.length-i-1)); } alert(dec);
22nd Sep 2017, 9:04 AM
visph
visph - avatar