Last Updated: June 7, 2026
The factorial of n is the product of every integer from 1 up to n, so 5! = 1 x 2 x 3 x 4 x 5 = 120. Two small inputs follow a fixed convention: 1! is 1, the single-element product, and 0! is also 1, the empty product. That keeps the identity n! = n x (n-1)! consistent down to n = 1.
That identity is the heart of the recursive view. 5! is 5 x 4!, 4! is 4 x 3!, and the chain continues until it reaches 0!, which is known directly.
The size of the result needs care. Factorials grow faster than exponentials. 12! is 479001600 and fits in a signed 32-bit integer, but 13! exceeds 2^31 - 1 and overflows. A signed 64-bit integer holds values through 20!, so the return type here is 64 bits wide. JavaScript and TypeScript store numbers as 64-bit floats, exact only up to Number.MAX_SAFE_INTEGER, and 18! is below that bound. To keep every language exact on the same inputs, the constraint stops at n <= 18.
n can be 0, and 0! is defined as 1. The base case must return 1 for 0.n can be 1, and 1! is 1. The recursion handles this with no special case, since factorial(1) returns 1 x factorial(0), which is 1 x 1 = 1.12!. The return type is 64-bit (long, long long, int64, i64) so values through 18! stay exact in every language.Factorial has a self-referential definition: n! = n x (n-1)!. That translates directly into a function that calls itself: to compute factorial(n), multiply n by factorial(n - 1) and let the same function resolve the smaller piece.
A recursion needs a base case that returns without recursing, or it calls itself forever. The base case here is factorial(0) = 1, the empty product. Each call lowers n by one, so the chain reaches 0 for any non-negative input. Once a call sees n = 0, it returns 1, and the pending multiplications complete on the way back up. This also covers n = 1, since factorial(1) returns 1 x factorial(0) = 1.
n is 0, return 1. This is the base case that stops the recursion.n multiplied by the result of calling the function with n - 1.Input:
The first call is factorial(5), which returns 5 x factorial(4). That waits on factorial(4), which returns 4 x factorial(3), and so on down to factorial(0), which returns 1 from the base case. Now the calls unwind: factorial(1) returns 1 x 1 = 1, factorial(2) returns 2 x 1 = 2, factorial(3) returns 3 x 2 = 6, factorial(4) returns 4 x 6 = 24, and factorial(5) returns 5 x 24 = 120. The base case stopped the descent at 0, and each pending multiplication completed on the way back up.
n down to 0, and each call does one multiplication.n pending calls sits on the call stack until the base case is reached and they unwind.Factorial is a running product, and a running product is built with an accumulator. Start a variable at 1, then walk through the integers 2, 3, ..., n and multiply the accumulator by each one. After the last multiplication, the accumulator holds n!. Starting at 1 gives the right answer for n = 0 and n = 1 for free, because the loop over 2, ..., n runs zero times and leaves the accumulator at its initial value.
result to 1.i from 2 up to n, multiply result by i.result.Input:
The accumulator starts at 1. Multiplying by 2 gives 2, by 3 gives 6, by 4 gives 24, and by 5 gives 120. The loop has reached n, so it stops and the answer is 120. The factors 2, 3, 4, 5 were applied from smallest to largest, the opposite order from the recursion, but the product is identical.
2 to n, doing one multiplication per step.n.