Concat in Use Recursion to Create a Range of Numbers

I am not sure why we need to concat endNum here:

function rangeOfNumbers(startNum, endNum) {
  return startNum === endNum
  ? [startNum]
  : rangeOfNumbers(startNum,endNum-1 ).concat(endNum);
};

This is the lesson

I don’t know what you want to use instead, but I’m assuming push() is in your mind. Well, push() and concat() have some fundamental differences in use cases and functionalities. push() is used to add elements to an existing array and it’ll always change the array whereas, concat() is used to merge arrays and it doesn’t change the existing array, rather returns a new array and if you look closely, it’s essential for the function in the following challenge to create and return a new array since the provided arguments don’t include an array that you can push elements into, and therefore, concat() is used. I’d suggest you do some research yourself instead, it’s not anything complicated.

1 Like

thank you so much. @Anindya

1 Like