title: about the scope of forEach method in Javascript
body: <p>I'm a beginner trying to learn to code in javascript. I'm sorry to ask such basic question but I've read a lot of ressources and I still struggle to understand the following behaviour about the scope of the forEach() method (and maybe other methods?).</p>
  <p>Consider an array of numbers. I want to calculate the sum of the numbers in the array.</p>
  <p>So I declare and initialize a variable called sum.</p>
  <p>Then I use the forEach() method to calculate the sum of the items and put the result in the sum variable</p>
  <p>Then I console.log(sum).</p>
  <p>What I don't understand is why console.log can access the sum variable inside the forEach() function? I thought it would print 0, because console.log is outside the local scope of the forEach() method. I understand the variable sum has global scope, but wouldn't the calculation inside forEach() have local scope?</p>
  <p>Thanks for your help.</p>
  <pre><code>let arrayOfNumbers = [1, 5 ,9, 16, 34, 45]
  
  let sum = 0;
  
  arrayOfNumbers.forEach(function(item) {
  
      sum = sum + item;
  })
  
  console.log(sum) //prints 110
  </code></pre>