Skip to content
Devtoolry

Numbers · beginner

JavaScript Program to Find the Factorial of a Number

Find the factorial of a number in JavaScript with a loop and with recursion, and use BigInt for exact results beyond 18!, where normal numbers lose precision.

The factorial of n, written n!, is the product of all whole numbers from 1 to n: 5! = 120, and by definition 0! = 1. It counts arrangements: five books can be ordered in 120 ways.

JavaScript numbers are 64-bit floating point, exact only up to Number.MAX_SAFE_INTEGER (9,007,199,254,740,991). 18! still fits, but 19! and above are rounded. BigInt values, written with an n suffix, are exact at any size.

The program

JavaScript
function factorial(n) {
  if (n < 0) throw new RangeError("factorial is not defined for negative numbers");
  let result = 1n;
  for (let i = 2n; i <= BigInt(n); i++) {
    result *= i;
  }
  return result;
}

console.log(factorial(5).toString());
console.log(factorial(0).toString());
console.log(factorial(25).toString());
Output
120
1
15511210043330985984000000

How it works

  1. Negative numbers have no factorial, so the function throws a RangeError with a clear message.
  2. result starts at 1n, a BigInt. It is also the answer for 0 and 1, because the loop does not run for them.
  3. The loop variable i is a BigInt too: BigInt and Number values cannot be mixed in arithmetic.
  4. toString() prints the digits; console.log of a BigInt in this compiler shows the n suffix, like 120n.

Watch it run line by line

Watch result grow as 1n, 2n, 6n, 24n, 120n while i counts up. Then visualize the recursive version below: each call adds a factorial frame, and the return values flow back up the stack.

“Visualize step by step” opens the program in the JavaScript Compiler and records every step in your browser. Use the arrow keys to move forwards and backwards; changed variables are highlighted.

Another way: Recursion, and why Number breaks

The recursive version follows the definition n! = n × (n - 1)!. With plain numbers, 25! comes out rounded (its last digits are wrong) because it is far above the safe integer range.

JavaScript
const factorial = (n) => (n <= 1 ? 1 : n * factorial(n - 1));

console.log(factorial(5));
console.log(factorial(18) <= Number.MAX_SAFE_INTEGER);
console.log(factorial(25));
Output
120
true
1.5511210043330986e+25

Common mistakes

  • Starting result at 0, which makes every factorial 0.
  • Mixing BigInt and Number: 1n * 2 throws "Cannot mix BigInt and other types". Convert with BigInt(n).
  • Trusting large Number results: 25! as a Number prints in exponent form and its exact digits are lost.
  • A recursive version without a base case runs until "Maximum call stack size exceeded".

Try it yourself

  • Print a table of the factorials from 0 to 20 with BigInt.
  • Count the trailing zeros of 100! by counting factors of 5 (the answer is 24).

Open the program in the JavaScript Compiler, change it and press Run: there is nothing to install, and your code never leaves your browser.

Browse all 25 JavaScript examples, or write your own program in the online JavaScript Compiler.