title: declaring variable inside while loop javascript
body: <p>I don't understand this code.</p>
  
  <pre><code>var timesTable;
  while ((timesTable = prompt("Enter the times table", -1)) != -1) {.....}
  </code></pre>
  
  <p>Why is necessary to declare the variable before? I tried to do this:</p>
  
  <pre><code>while ((var timesTable = prompt("Enter the times table", -1)) != -1) {.....}
  </code></pre>
  
  <p>but it doesn't work. What's the problem? Is something about scope?</p>
  
  <p>The full program is:</p>
  
  <pre><code>function writeTimesTable(timesTable, timesByStart, timesByEnd) {
          for (; timesByStart &lt;= timesByEnd; timesByStart++) {
              document.write(timesTable + " * " + timesByStart + " = " + timesByStart * timesTable + "&lt;br /&gt;");
              }
          }
      var timesTable;
      while ((timesTable = prompt("Enter the times table", -1)) != -1) {
          while (isNaN(timesTable) == true) {
              timesTable = prompt(timesTable + " is not a " + "valid number, please retry", -1);
          }
          document.write("&lt;br /&gt;The " + timesTable + " times table&lt;br /&gt;");
          writeTimesTable(timesTable, 1, 12);
      }
  </code></pre>
  
  <p>Thanks by advance.</p>