Using getElementById attribute you can Pass JavaScript variable to HTML input value.
Input box
<input type="text" id="mytext">JavaScript code
document.getElementById("mytext").value = msg;Pass variable to HTML input value using JavaScript
Simple example code.
<!DOCTYPE html>
<html>
<body>
  <input type="text" id="mytext">
  <script type="text/javascript">
    var msg = "Hello World!";
    document.getElementById("mytext").value = msg;
  </script>
</body>
</html>Output:

Do comment if you have any doubts or suggestions on this JS variable topic.
Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.
OS: Windows 10
Code: HTML 5 Version
very nice post