Skip to content

Latest commit

 

History

History
19 lines (12 loc) · 656 Bytes

what_is_a_proper_tail_call.md

File metadata and controls

19 lines (12 loc) · 656 Bytes

What is a Proper Tail Call?

A Proper Tail Call (PTC) is a feature where the last action of a function is a call to another function, and the current function's stack frame is removed before executing the called function. This optimization prevents stack overflow errors for recursive calls.

Example:

function factorial(n, acc = 1) {
  if (n === 0) return acc;
  return factorial(n - 1, acc * n);  // Tail call
}
console.log(factorial(5));  // Output: 120

Tags: advanced, JavaScript, Optimization