splice is affecting previously copied variables
<blockquote>
  <p><strong>Possible Duplicate:</strong><br>
  <a href="http://stackoverflow.com/questions/7486085/copying-array-by-value-in-javascript">Copying array by value in javascript</a>  </p>
</blockquote>



<p>i have a funny problem with javascript. i copy an array variable to make modifications on the copy only, then splice the copy to delete an element. however the original array variable is affected by the splice - as if the copy was a 'copy by reference':</p>

<pre><code>window.onload = function() {
  var initial_variable = ['first', 'second', 'third'];
  var copy_initial_variable = initial_variable;
  copy_initial_variable.splice(0, 1);
  alert('initial variable - ' + initial_variable);
};
//output: initial variable - second,third
</code></pre>

<p><strong>firstly</strong>, is this intentional behaviour for javascript or is it a bug?</p>

<p><strong>and secondly</strong>, how can i make a copy of an array and delete an element in the copy but not in the original?</p>

<p>one thing which makes me think that the above may be a javascript bug is that this behaviour only happens with arrays and not with integers. for example:</p>

<pre><code>window.onload = function() {
  var initial_variable = 1;
  var copy_initial_variable = initial_variable;
  copy_initial_variable = 2;
  alert('initial variable - ' + initial_variable);
};
//output: initial variable - 1
</code></pre>

<p>if the behaviour were consistent then this ought to output <code>2</code> since the assignment would presumably be by reference?</p>
