title: Best alternative to using the global variable in this code
body: <p>Since I started programming, I seem to be aiming towards global variables, and find it difficult to work without them, and found it especially difficult here with a recursive function. Can someone help me with this code to make it global variable free? Also, is there a reference which can help change the method of thinking so I do not end up feeling the need for them? I feel something in the way I think about code needs to change, but I don't know what.</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>var res = [];
  
  function steamrollArray(arr) {
    // I'm a steamroller, baby
    func(arr);
    console.log(res);
    return res;
  }
  
  steamrollArray([1, {}, [3, [[4]]]]);
  
  function func(array){
    for (var i=0; i&lt;array.length ; i++){
      if (Array.isArray(array[i])){
        func(array[i]);
      }
      else {
        res.push(array[i]);
      }
    }
  }</code></pre>
  </div>
  </div>
  </p>