Programming concepts

Recursion

Try the free JavaScript compiler

A recursive function calls itself directly or through another function. A reachable base case and progress toward it are needed for termination. Large depths may exceed runtime stack limits.

Formal Syntax & Structure

javascript syntaxjavascript
function f(n) { if (base) return result; return f(smaller); }

Working Example

Example Snippetjavascript
function f(n){if(n===0)return 0;return n+f(n-1);}console.log(f(4));
JavaScript Reference