How Recursion Works — explained with flowcharts and a video
Beau Carnes
3.6K19

Is there anything wrong with writing the first example like this?

function countdown(i) {
if (i <= 0)
return;

console.log(i);
countdown(i - 1);
}
countdown(5);

As mentioned previously, the else statement in the article’s example seems to be redundant. If your recursive function is much more complicated, you can end up with many levels of nesting if every “don’t return” case is in the body of an else .

Here is the example I’m talking about for reference.

function countdown(i) {
console.log(i)
if (i <= 1) { // base case
return;
} else { // recursive case
countdown(i - 1)
}
}
countdown(5);    // This is the initial call to the function.