Factorial of a Number in JavaScript
Find the factorial of a number in JavaScript with a for loop, recursion, while and reduce. Learn why 0! is 1 and when to use BigInt. Explanation and output.
1// Factorial: n! = n × (n-1) × (n-2) × ... × 2 × 12// Example: 5! = 5 × 4 × 3 × 2 × 1 = 12034// Method 1: Using for loop5function factorialLoop(n) {6 if (n < 0) return "Invalid input";7 if (n === 0 || n === 1) return 1;89 let result = 1;10 for (let i = 2; i <= n; i++) {11 result *= i;12 }13 return result;14}1516console.log("5! =", factorialLoop(5));17console.log("0! =", factorialLoop(0));18console.log("10! =", factorialLoop(10));1920// Method 2: Using recursion21function factorialRecursive(n) {22 if (n < 0) return "Invalid input";23 if (n === 0 || n === 1) return 1;24 return n * factorialRecursive(n - 1);25}2627console.log("\nUsing recursion:");28console.log("5! =", factorialRecursive(5));29console.log("7! =", factorialRecursive(7));3031// Method 3: Using while loop32function factorialWhile(n) {33 if (n < 0) return "Invalid input";34 if (n === 0 || n === 1) return 1;3536 let result = 1;37 let i = n;38 while (i > 1) {39 result *= i;40 i--;41 }42 return result;43}4445console.log("\nUsing while:");46console.log("6! =", factorialWhile(6));4748// Method 4: Using reduce49function factorialReduce(n) {50 if (n < 0) return "Invalid input";51 if (n === 0 || n === 1) return 1;5253 return Array.from({length: n}, (_, i) => i + 1)54 .reduce((product, num) => product * num, 1);55}5657console.log("\nUsing reduce:");58console.log("4! =", factorialReduce(4));
Sample Output
5! = 120 0! = 1 10! = 3628800 Using recursion: 5! = 120 7! = 5040 Using while: 6! = 720 Using reduce: 4! = 24
In this one
- What a factorial means
- Why 0! is 1 and the result starts at 1
- How recursion and a base case work
- Why large factorials need BigInt
How it works
- 1
factorialLoop(5) multiplies 1 × 2 × 3 × 4 × 5 = 120.
- 2
factorialLoop(0) returns 1 at once.
- 3
factorialRecursive(7) calls itself down to 1 and returns 5040.
- 4
factorialWhile(6) counts down from 6 and returns 720.
- 5
factorialReduce(4) returns 24.
factorialRecursive(4)
How the recursive calls stack up and return.
- 1.4 * factorialRecursive(3)
- 2.3 * factorialRecursive(2)
- 3.2 * factorialRecursive(1)
- 4.factorialRecursive(1) returns 1
- 5.Back up: 2 * 1 = 2, 3 * 2 = 6, 4 * 6 = 24
Edge cases
Negative input
factorialLoop(-3) returns "Invalid input".
Returning a string mixes types. In real code you might throw an error instead.
Large n
factorialLoop(25) prints 1.5511210043330986e+25.
From 19! on, factorials exceed Number.MAX_SAFE_INTEGER. That is the limit below which every whole number is exact; some larger values are still exact, but not all. Use BigInt for guaranteed exact factorials.
Deep recursion
factorialRecursive(100000) throws RangeError: Maximum call stack size exceeded.
Each call uses stack memory. Use the loop for very large n.
What's going on
The factorial of n, written n!, is n × (n - 1) × ... × 1. So 5! is 5 × 4 × 3 × 2 × 1 = 120. Factorials count arrangements. If 5 students stand in a line, there are 120 different orders.
Two special values come first. 0! is 1, not 0. That is the math definition, and it makes formulas work. Negative numbers have no factorial, so every method here returns "Invalid input" for n < 0.
Method 1 uses a for loop. result starts at 1, not 0. If it started at 0, every multiplication would give 0. The loop multiplies result by 2, 3, 4 and so on up to n.
Method 2 uses recursion. Recursion means a function calls itself. factorialRecursive(5) returns 5 × factorialRecursive(4), which returns 4 × factorialRecursive(3), and so on. The base case, n === 0 || n === 1, stops the chain. Without a base case the function would call itself forever and crash with a stack overflow.
Method 3 counts down with while. Method 4 builds [1, 2, ..., n] and multiplies with reduce, starting from 1.
Factorials grow very fast. From 19! on, results are larger than Number.MAX_SAFE_INTEGER, so JavaScript can no longer promise exact whole numbers. For exact big factorials, use BigInt with values like 21n.
Quiz
Tap an option. The answer stays hidden until you pick.
Question 1
What is 4!?
Pick an answer to check it.
Question 2
What stops a recursive function from running forever?
Pick an answer to check it.
Questions people ask
Why is 0! equal to 1?
There is exactly one way to arrange zero items: do nothing. It also keeps formulas consistent.
Is recursion slower than a loop?
Usually a little, and it can run out of stack. The loop is the safer default.
How do I compute 25! exactly?
Use BigInt: start result at 1n and multiply by BigInt(i).