In ES6 new features, I cannot understand the "Tail Calls", Calls in tail-position are guaranteed to not grow the stack unboundedly. Makes recursive algorithms safe in the face of unbounded inputs.
function factorial(n, acc = 1) {
'use strict';
if (n <= 1) return acc;
return factorial(n - 1, n * acc);
}
// Stack overflow in most implementations today,
// but safe on arbitrary inputs in ES6
factorial(100000)
I searched but I still cannot understand clearly.
Can anyone explain to me why the old javascript(ES5) will have problems with recursive algorithm?
How the "Tail Calls" avoid the problem?