title: Reassigning global variable in JavaScript - Can somebody explain to me why currentAcc stays undefined
body: <p>so basically I tried the same thing in a simple test scenario without .addEventlistener() and it works but I do not get this to work, <strong>currentAcc</strong> stays undefined in the global scope and does not takes the assigned value of the function. I would much appreciate some help.</p>
  <pre><code>let currentAcc;
  
  function logIn() {
    function logInCurrAcc(event) {
      event.preventDefault();
  
      currentAcc = accounts.find((acc) =&gt; acc.username === usernameInput.value);
      console.log(currentAcc);
  
      if (currentAcc.password === passwordInput.value) {
        window.location.assign(&quot;app.html&quot;);
      } else {
        alert.classList.remove(&quot;hidden&quot;);
        setTimeout(function () {
          alert.classList.add(&quot;hidden&quot;);
        }, 3000);
      }
    }
  
    submitFormBtn.addEventListener(&quot;click&quot;, logInCurrAcc);
  }
  
  console.log(currentAcc);
  
  // Initializes everything
  function init() {
    if (document.body.contains(tabsContainer)) {
      tabsComponent();
      sliderComponent();
      modal();
      logIn();
    } else {
      console.log(&quot;Not loading init&quot;);
    }
  }
  
  init();```
  </code></pre>