I have always viewed recursive functions with a bad eye, and I have rejected them in favor of other options.
Why would I do this?:
foo(0);
function foo(cont){
console.log(cont);
cont++;
if(cont<50){
foo(cont);
}
}
Being able to do this:
foo();
function foo(){
for(let cont=0;cont<50;cont++){
console.log(cont);
}
}
I have searched for information, and I have found that recursive functions can be used to optimize Tail Call Optimization
Recursion functional javascript
To which I do not find much sense, if in the previous snippets we simulate a larger loop for example: cont < 99999
. It función recursiva
fails before reaching the end while he for
completes it.
And on the other hand, if we simulate an infinite loop, the for
does not do it while the función recursiva
does try and again gives an error.