Skip to content

Instantly share code, notes, and snippets.

@Ynote
Ynote / tail-call-optimization.js
Last active October 18, 2022 13:33
[Javascript] - Tail-recursive factorial function
// This is just a short reminder of this great explanation:
// http://www.2ality.com/2015/06/tail-call-optimization.html
// not TCO
function factorial(n) {
if (n <= 0) return 1;
return n * factorial(n-1); // here, the main recursive call not in a tail position because of the `n` context.
}