Quick question on array.prototype.concat()

I have a question on my code, it’s the one line return I use and the one below. What is the Difference, if any?

var globalArray = [5, 6, 3, 2, 9];
function nonMutatingSort(arr) {
  // Add your code below this line  

  return arr.concat().sort((a, b) => a - b);
  // Add your code above this line

or using:

return [].concat(arr).sort((a, b) => a - b);

I passed the challenge with the first one, just trying to understand concat() on a deeper level.

@orb You can read the MDN documentation on concat and see that if you do not specify an arguments when calling the method, then it returns a shallow copy of the array. The concat method always returns a new array instance.

1 Like