Suggest an editImprove this articleRefine the answer for “Deep recursion”. Your changes go to moderation before they’re published.Approval requiredContentWhat you’re changing🇺🇸EN🇺🇦UAPreviewTitle (EN)Short answer (EN)With **deep recursion** (when a function calls itself many times before reaching the base case), JavaScript can throw a call stack overflow - a `RangeError: Maximum call stack size exceeded` error. **Key point:** each function call creates a new context on the call stack, and the stack has a limited size, usually around 10,000-20,000 nested calls.Shown above the full answer for quick recall.Answer (EN)ImageWith **deep recursion** (that is, when a function calls itself **many times** before reaching the base case), JavaScript can produce a **call stack overflow** - a `RangeError: Maximum call stack size exceeded` error. --- ## What happens "under the hood" Every time a function calls itself, JS **creates a new execution context** (a stack frame) on the **call stack**: ```javascript factorial(5) → factorial(4) → factorial(3) → factorial(2) → factorial(1) ``` Each call stores: - local variables, - arguments, - the return address. When the base case is reached, the stack "unwinds" back. But if there are **too many calls**, the stack overflows. --- ## Example of a stack overflow ```javascript function recurse(n) { console.log(n); recurse(n + 1); // no base case! } recurse(1); ``` Error: ```javascript RangeError: Maximum call stack size exceeded ``` The browser (or Node.js) allocates a limited stack size, usually around **10,000-20,000** nested calls. --- ## Even with a base case, you can hit the limit ```javascript function countdown(n) { if (n === 0) return; countdown(n - 1); } countdown(100000); // RangeError ``` Despite having a base case, the recursion depth (100,000) is too large for the JS stack. --- ## How to avoid the problem **1. Rewrite the recursion as a loop** ```javascript function countdown(n) { while (n > 0) n--; } ``` **2. Use tail recursion (if it were optimized)** ```javascript function countdown(n) { if (n === 0) return; return countdown(n - 1); // tail call } ``` But: most JS engines **do not implement tail call optimization**. **3. Split the recursion into "chunks" via** `setTimeout` ```javascript function countdown(n) { if (n === 0) return; console.log(n); setTimeout(() => countdown(n - 1), 0); // does not block the stack } ``` Here each call runs **on a new event loop iteration**, so the stack does not grow. --- ## Summary | Phenomenon | What happens | |---|---| | Deep recursion | Many nested calls of the same function | | Result | Stack overflow (`RangeError`) | | Why | Each call creates a new context on the stack | | How to avoid it | Use a loop, tail recursion, or `setTimeout` |For the reviewerNote to the moderator (optional)Visible only to the moderator. Helps review go faster.