title: Why does changing an Array in JavaScript affect copies of the array?
body: <p>I've written the following JavaScript:</p>
  
  <pre><code>var myArray = ['a', 'b', 'c'];
  var copyOfMyArray = myArray;
  copyOfMyArray.splice(0, 1);
  alert(myArray); // alerts ['b','c']
  alert(copyOfMyArray); // alerts ['b','c']
  
  var myNumber = 5;
  var copyOfMyNumber = myNumber;
  copyOfMyNumber = copyOfMyNumber - 1;
  alert(myNumber); // alerts 5
  alert(copyOfMyNumber); // alerts 4        
  </code></pre>
  
  <p>This code declares a variable <code>myArray</code> and sets it to an array value.  It then declares a second variable <code>copyOfMyArray</code> and sets it to <code>myArray</code>.
  It performs an operation on <code>copyOfMyArray</code> and then alerts both <code>myArray</code> and <code>copyOfMyArray</code>.  Somehow, when I perform an operation on <code>copyOfMyArray</code>, it appears that the same operation is performed on <code>myArray</code>.</p>
  
  <p>The code then does the same thing with a number value:  It declares a variable <code>myNumber</code> and sets it to a number value.  It then declares a second variable <code>copyOfMyNumber</code> and sets it to <code>myNumber</code>.  It performs an operation on <code>copyOfMyNumber</code> and then alerts both <code>myNumber</code> and <code>copyOfMyNumber</code>.  Here, I get the expected behavior: different values for <code>myNumber</code> and <code>copyOfMyNumber</code>.</p>
  
  <p>What is the difference between an array and a number in JavaScript that it seems changing an array changes the value of a copy of the array, where as changing a number does not change the value of a copy of the number?</p>
  
  <p>I'm guessing that for some reason, the array is referred to by reference and the number by value, but why?  How can I know what behavior to expect with other objects?</p>