title: JavaScript Variables Not Defining Correctly Inside Function
body: <p>I'm new to JS and am trying to create a simple 'swap array elements if array A element is bigger than array B element' function. In the <code>swapIndexes</code> function, I don't understand why I can't define the variables as shown in the comments. For example, it works if it state <code>arrA[c]</code> rather than <code>let a = arrA[c]</code>.</p>
  
  <p>Why does this happen? Can anyone give some beginner tips on how best to go about something like this? My code feels verbose. Thanks for any help here.</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 arrA = [0, 1, 2, 7, 6],
    arrB = [0, 1, 2, 5, 7],
    indexesToSwap = [],
    aValuesToSwap = [],
    bValuesToSwap = [],
    needSwapping = false;
  
  arrA.forEach(getSwappableIndexesAndValues);
  indexesToSwap.forEach(swapIndexes);
  
  function getSwappableIndexesAndValues(c, i) {
    let b = arrB[i];
  
    if (c &gt; b) {
      needSwapping = true;
      indexesToSwap.push(i);
      aValuesToSwap.push(b);
      bValuesToSwap.push(c);
    }
  }
  
  function swapIndexes(c, i) {
    //let a = arrA[c];  fails why???
    //let b = arrB[c];  fails why???
    //a = aValuesToSwap[i];  fails why???
    //b = bValuesToSwap[i];  fails why???
  
    arrA[c] = aValuesToSwap[i];
    arrB[c] = bValuesToSwap[i];
  }
  
  console.log(arrA);
  console.log(arrB);</code></pre>
  </div>
  </div>
  </p>