title: Built-in function to convert binary to decimal in JavaScript
body: <p>I'm writing a program to convert binary numbers to decimal numbers in JavaScript. I am wondering if there is a built in way to do this without having to write all of the manual logic. Here's my code:</p>
  
  <pre><code>var from = $("#from").val();
  var to = $("#to").val();
  var input = $("#input").val().toString();
  var output = "";
  
  var invalid = false;
  if (input == "") {
      $("#invalid").text("Please enter a number in the input field")
      invalid = true;
  }
  
  if (from == "bin" &amp;&amp; to == "dec") {
      // check if valid binary digits
      for (var i = 0; i &lt; input.length; i++) {
          if (input.charAt(i) != '1' &amp;&amp; input.charAt(i) != '0') {
              $("#invalid").text("You did not enter a valid binary number. Please try again!")
              invalid = true;
          }
      }
  
      if (!invalid) {
          // QUESTION: find a clean way to convert
      }
  }
  </code></pre>