title: Should I declare a variable using LET globally for a function that runs constantly?
body: <p>Most relevant answers here do not refer to <em>let</em>, which is block scoped, but to <em>var</em>, which is hoisted. I can't seem to get a definitive answer to this.</p>
  
  <p>I have a variable declared globally that initializes once:</p>
  
  <pre><code>let firePaused = false;
  </code></pre>
  
  <p>and then a function in the keyboard handler that is running everytime I press a button:</p>
  
  <pre><code>function actOnKeyPress() {
    if (rightPressed) {
      game.hero.rotate(game.hero.speed);
    } else if (leftPressed) {
      game.hero.rotate(-game.hero.speed);
    }
    if (!firePressed) {
      firePaused = false;
    }
    if (firePressed &amp;&amp; options.numberOfBullets &gt; 0) {
      if (!firePaused) {
        fireBullet();
        firePaused = true;
      }
    }
  }
  </code></pre>
  
  <p>(not relevant to the question, but it's purpose is to only allow the player to fire once, there needs to be a keyup event before they can fire again)</p>
  
  <p>By the rules of clean code I should declare the variable at the top of the function... BUT that means it will be redeclared everytime I press a button.</p>
  
  <p>It says here <a href="https://www.sitepoint.com/how-to-declare-variables-javascript/" rel="nofollow noreferrer">https://www.sitepoint.com/how-to-declare-variables-javascript/</a></p>
  
  <blockquote>
    <p>Initialization: When you declare a variable it is automatically
    initialized, which means memory is allocated for the variable by the
    JavaScript engine.</p>
  </blockquote>
  
  <p>So I would be creating a whole new variable each time the <em>let</em> keyword is used.</p>
  
  <p>Should I go to the bother of writing a conditional at the start of the function to check if <code>firePaused</code> is declared yet, and if not declare it? That seems total overkill.</p>