I was just experimenting with some recursion and noticed something that confused me. Let me illustrate with some code examples:
function loop(x) {
if (x >=
If you don't return from a Javascript function there is an implicit "return undefined" in the end.
function loop(x) {
if (x >= 10)
return x;
loop(x + 1); // the recursive call
return undefined;
}
As you can see, te recursive call is being called and having its return value ignored. This is just like what happens when you call a function like console.log
- the function gets called and runs any side-effects but you discard the return value in the end.