What is recursion in JavaScript?
When a function calls itself. It requires a base case (when to stop) and a recursive case (call with a smaller input). Use for: tree traversal, factorial, fibonacci, divide-and-conquer.
Verify This Answer
Cross-check this information using these trusted sources:
More FAQs in Thinking Recursively in JavaScript
The condition that stops the recursion. Without a base case, the function calls itself forever, causing a stack overflow (RangeError: Maximum call stack size exceeded). The base case is the simplest version of the problem.
1. Identify the base case (when to stop). 2. Identify the recursive case (how to reduce the problem). 3. Trust the recursion (assume the smaller call works). 4. Combine the result with the current step.
Tree traversal (DOM, JSON, file systems), divide-and-conquer (merge sort, quick sort), backtracking (permutations), and problems with self-similar structure (factorial, fibonacci, fractals).
Still have questions?
Browse all our FAQs or reach out to our support team
