title: function return undefined javascript
body: <p>I am new to javascript and I try to make a simple game with level. But my code doesn't running like I want. I see many questions on the forum like my problem but I don't understand thanks to them why my function doesn't return anyhting. Can you explain me why, and not only give me the solution.
  Thanks !</p>
  
  <pre><code>function level()
  {
      var SquareNbrLevel;
      var levelBtn = document.querySelectorAll(".level-btn"); //Select all levels button
  
      for(var i = 0; i &lt; levelBtn.length; i++)
      {
          levelBtn[i].addEventListener("click", function()
          {
              if(this.textContent === "Easy")
              {
  
                  SquareNbrLevel = 3;
                  console.log("easy " + SquareNbrLevel);
                  return SquareNbrLevel;
  
              }
              else
              {
                  SquareNbrLevel = 6;
                  console.log("hard " + SquareNbrLevel);
                  return SquareNbrLevel;
              }
  
          });
      }
  
  }
  
  
  var nbr  = level();
  console.log("Level" + nbr);
  </code></pre>