title: Change the value of an array changes original array JavaScript
body: <p>The following code causes both elements from id <code>0</code> to be set to <code>-</code>, even though I want only one to be set to <code>-1</code>. Am I just creating a reference to the labelArray, or is something else?</p>
  
  <pre><code>labelArray.sort(compare);
  valueArray = labelArray;
  valueArray[0] = '-1';
  labelArray[0] = '-';
  </code></pre>
  
  <p>All help is appreciated.</p>
  
  <p>UPDATE (2019): It's been several years since I first did this post, and ES6 is used pretty much universally. So, I wanted to come back and add that, instead of using the <code>slice()</code> method recommended in the accepted answer, you can instead use <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment" rel="nofollow noreferrer">array destructing</a> in the following to make a copy:</p>
  
  <pre><code>valueArray = [...labelArray];
  </code></pre>