JavaScript Program to Find Factorial of a Number using Recursion
Last Updated :
23 Jul, 2025
In this article, we are going to learn about finding a Factorial of a Number using Recursion. Calculating the factorial of a number using recursion means implementing a function that calls itself to find the factorial of a given number. The factorial of a non-negative integer "n" is the product of all positive integers less than or equal to "n".
The factorial of a positive number n is given by:
factorial of n (n!) = 1 * 2 * 3 * 4 * . . . * n
Example:
Input : N = 6
Output : The factorial of given number is : 720
Explanation: 6 * 5 * 4 * 3 * 2 * 1 = 720
Hence factorial of 6 is : 720
There are some common approaches that can be used to Find Factorial of Number Using Recursion, which is listed below:
- Factorial using Recursion in JavaScript
- Using Ternary Operator with Recursive Approach
We will explore all the above methods along with their basic implementation with the help of examples.
To calculate the factorial of a number "N" using recursion, we first establish base cases: if N is 0 or 1, the factorial is 1. Otherwise, we recursively compute N * factorial(N-1) to find the product of all positive integers up to N.
Syntax:
function factorial(n) {
if (n === 0 || n === 1) {
// Code...
}
}
Example: In this example, we are using the above-explained approach.
JavaScript
function factorial(n) {
if (n === 0 || n === 1) {
return 1;
} else {
return n * factorial(n - 1);
}
}
let num1 = 6;
let result = factorial(num1);
console.log("The factorial of given number is :" + result);
OutputThe factorial of given number is :720
Approach 2: Using Ternary Operator with Recursive Approach
In this approach the recursive function uses the ternary operator to check if the base case (num === 0) is met, returning 1, otherwise calls itself with (num - 1) and multiplying num with the result.
Syntax:
condition ? value if true : value if false
Example: In this example, we are using the above-explained approach.
factorial of 10 is, 10! = 10*9*8*7*6*5*4*3*2*1 = 3628800
JavaScript
function factorialFunction(num) {
return num === 0 ? 1 : num * factorialFunction(num - 1);
}
const num1 = 10;
const result = factorialFunction(num1);
console.log(result);
Similar Reads
JavaScript Program to Print N to 1 using Recursion In this article, we will see how to print N to 1 using Recursion in JavaScript. What is Recursion? The process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called a recursive function. In the recursive program, the solution to the base
1 min read
JavaScript Program to Print 1 to N using Recursion In this article, we will see how to print 1 to N using Recursion in JavaScript. What is Recursion? The process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called a recursive function. In the recursive program, the solution to the base
2 min read
JavaScript Program to Find the Sum of Digits in a Factorial Let's first define the problem before proceeding to a solution. Any positive number less than or equal to n is the product of all positive integers, and the factorial of a non-negative integer n is denoted by n!. For example, 5!, sometimes known as "five factorial," is equivalent to 5 Ã 4 Ã 3 Ã 2 Ã
3 min read
JavaScript Program to Display Fibonacci Sequence Using Recursion In this article, we will explore how to display the Fibonacci sequence using recursion in JavaScript. The Fibonacci sequence is a series of numbers where each number is the sum of the two preceding ones. Recursion is a powerful technique in programming that involves a function calling itself. A recu
3 min read
Java Program to Find Factorial of a Number Recursively Factorial of a number n is defined as a product of all positive descending integers, Factorial of n is denoted by n!. Factorial can be calculated using the following recursive formula where the recursive call is made to a multiplicity of all the numbers lesser than the number for which the factorial
4 min read
C++ Program to Find Factorial Using Recursion The factorial of a number is denoted by "n!" and it is the product of all positive integers less than or equal to n. In this article, we will learn how to find the factorial of a number using recursion in C++. Example Input: 5 Output: Factorial of 5 is 120 Factorial Using Recursion in C++The Factori
2 min read