This tutorial explains how to find the factorial of a number using recursion in JavaScript.
The factorial of a positive number n is given by:
factorial(n) = 1*2*3*...*n
or,
factorial(n) = n * factorial(n-1) //using recursion method
Example 1: Find Factorial of a Number n using Recursion
// program to find the factorial of a number
function Factorial(n) {
if (n == 0) {
return 1;
}
else {
return n * Factorial(n - 1);
}
}
console.log(Factorial(5))
console.log(Factorial(4))
Output
120
24