0

I am working on understanding recursion. I wrote a function that returns the sum of an array.

If the if statement for the base case looks like this if (arr.length === 0), the function works. If it looks like this if (arr === []), I get a stack overflow. Why is that? Here is the full program.


const getSum = function (arr){

    //the if statement in question
    if (arr.length === 0){
        return 0;
    }
    else{
        console.log(arr)
        return arr[0] + getSum(arr.slice(1));
    };

};
  • _"If it looks like this if (arr === []), I get a stack overflow. Why is that?"_ - Because this is not how you check if an array is empty... – Andreas Jan 03 '23 at 13:11

0 Answers0