title: QuickSort infinite loop if I declare param defaults inside function using pre-2015 method, but works fine if I use ES2015 default param values
body: <p>I have been trying to implement a quickSort function and have everything working. But there is one peculiarity that I cannot wrap my head around or understand why.</p>
  
  <p>In this first block of code, you will see that I have declared some default param values for the <code>quickSort()</code> function:</p>
  
  <pre><code>function swap(arr, firstIndex, secondIndex) {
    let temp = arr[firstIndex];
    arr[firstIndex] = arr[secondIndex];
    arr[secondIndex] = temp;
  }
  
  function pivot(arr, start = 0, end = arr.length - 1) {
    // We are assuming that the pivot is always the first element
    let pivot = arr[start];
    let swapIndex = start;
  
    for (let i = start + 1; i &lt;= end; i++) {
      if (pivot &gt; arr[i]) {
        swapIndex++;
        swap(arr, swapIndex, i);
      }
    }
  
    // Swap the starting element with the pivot index
    swap(arr, start, swapIndex);
    return swapIndex;
  }
  
  function quickSort(arr, left = 0, right = arr.length - 1) {
    if (left &lt; right) {
      let pivotIndex = pivot(arr, left, right);
      // left
      quickSort(arr, left, pivotIndex - 1);
      // right
      quickSort(arr, pivotIndex + 1, right);
    }
    return arr;
  }
  </code></pre>
  
  <p>In this example, it works fine as expected. However, if I were to remove the ES2015 default param values from <code>quickSort()</code> and, instead, create the defaults inside of the function, like this:</p>
  
  <pre><code>function quickSort(arr, left, right) {
    left = left || 0;
    right = right || arr.length -1;
    if (left &lt; right) {
      let pivotIndex = pivot(arr, left, right);
      // left
      quickSort(arr, left, pivotIndex - 1);
      // right
      quickSort(arr, pivotIndex + 1, right);
    }
    return arr;
  }
  </code></pre>
  
  <p>I get an infinite loop/Stack Overflow issue and I cannot understand why. From what I can tell, the issue is caused by the third param - <code>right</code> - rather than the left param, as the code works fine if I call the left param using the pre-es2015 method, whilst leaving the <code>right</code> param with the ES2015 param default method.</p>
  
  <p>All in all, I have my code working, so that's fine - I just want to try and better understand why this would cause an issue, as I've never encountered such a problem before.</p>
  
  <p>Thanks!</p>