title: javascript - copy array inside function
body: <p>I'm having problems with the next code:</p>
  
  <pre><code>test1 = [0,0];
  function func(test)
  {
      var test2 = new Array();
      for(var i = 0; i &lt; test.length; i++)
      if(test[i] == 0)
      {
          test[i] = 1;
          test2.push(test);
          test[i] = 0;
      }
      return test2;
  }
  a = func(test1);
  document.write(a[0].toString()+"&lt;br/&gt;");
  document.write(a[1].toString());
  </code></pre>
  
  <p>and the output is:</p>
  
  <pre><code>0,0
  0,0
  </code></pre>
  
  <p>I have already checked with the console: when I change test[i] inside the condition, after test2.push(test), test2[test2.length] is also changed. (WHY?)</p>
  
  <p>Is there a scope problem?
  What should I do to get an output like this?</p>
  
  <pre><code>1,0
  0,1
  </code></pre>
  
  <p>Thanks.</p>