# JavaScript Factorial

Looking for factorial calculator or Javascript factorial? Finally, another post on finding factorial of a number using javascript. In this tutorial, we will try to find 52 factorial, zero factorial, and 20 factorial.

What is Factorial?
In mathematics, the factorial of an integer x is the product of all positive integers less than equal to x, where x is a natural number.

Factorial logic: let the number be x. Therefore, x!= x(x-1)*(x-2)*(x-3)*....*1

For example, the factorial of 5! is 120. By definition 5!=5*4*3*2*1=120

## Let write a function for JavaScript factorial:

Let's define a function which accepts a number and calculate it's factorial.

We can pass any value to the function by calling it.  You can put any positive value to the variable x to calculate the factorial of a number.

Let's assign 5 to x to find 5 factorial and test the function.
The output of 5 factorial on the console- As you can see that the javascript factorial function which we built is working fine.

## Finding factorial using javascript factorial function:

As a result, the function works fine till the value of x=21 but fail once we go beyond x value greater than 21.

For example, let's find the factorial of 22  using the above function. Have a look at the output above where we try to find the factorial of x=22 which is greater than 21.
Furthermore, you can see that the output of factorial 22! to be 1.1240007277776077e+21 but the actual answer should be 1124000727777607680000.

## Why the javascript factorial function is not giving the correct answer?

Our factorial logic is correct so it must have some problem with javascript for storing very large value.
Here is the reason why-
1. JavaScript Numbers are Always 64-bit Floating Point.
2. Integers (numbers without a period or exponent notation) are accurate up to 15 digits.

So, let's find an alternative approach as we are not satisfied with the result for a greater value of x.

I found a great article and approach to this problem by Niel Patel. Niel has beautifully explained the approach as solved this challenging problem.

The above function makes use of another function which is add():

## Factorial Calculator:

Also, I have made the javascript factorial calculator using the codes I mentioned above.

Kindly use this factorial calculator to find the factorial of any positive number.

Hence, This calculator can calculate factorial of large numbers such as 52 factorial, zero factorial, and 20 factorial.

## What is 52 factorial?

52 factorial is 80658175170943878571660636856403766975289505440883277824000000000000

The javascript factorial program works fine though the factorial of 52 is a significantly large value.

## what is zero factorial?

factorial of 0 is 1

#### Why zero factorial is 1?

Zero factorial is 1 because by recursive definition of factorial i.e  x!=(x+1)!/(n+1)

As a result, after initializing x with 0 we get the factorial of 0 as 0!=(0+1)!/(0+1).. i.e=1.

## What is 20 factorial?

Similarly, let's find the 20 factorial using the factorial calculator which I have made using javascript.

20 factorial is  2432902008176640000.

Go ahead and try with a different number, use the factorial calculator.

## Closing Notes:

Finally, thanks for reading, if the tutorial was helpful to you feel free to comment and don't forget to share.

Also if you are working towards becoming a better JavaScript developer  you can read javascript and jquery for front end web development