Recursive Functions: The Never-Ending Story

				
					public function factorial(n) {
						if (n == 0) {
							return 1;
						} else {
							return n * factorial(n-1);
						}
					}
				
				
					// Example usage:
					console.log(factorial(5)); // Output: 120
				
			
See Also: Tail Recursive Functions Or: Lambda Functions (for the truly adventurous)