title: javascript Cannot initialize global variable
body: <p>I'm a beginner in js and while experimenting I noticed something strange in my app :</p>
  <pre><code>var pin = &quot;&quot;;
  var inputField = document.querySelector('.pin'); // initialize globally 
  
  
  const addNum = (num)=&gt;{
    //let  inputField = document.querySelector(&quot;.pin&quot;); //code works by initializing this 
    pin+=num;
    inputField.value = pin; //error hits 
  }
  </code></pre>
  <p>If I initialize my inputField variable globally I get the error <code>Uncaught TypeError: Cannot set property 'value' of null</code> in my app . However when I declare and initialize the variable in my function I get no null error and my code runs . Why does this happen if my variable is globally scoped how can it be null?</p>