Updated April 15, 2023

## Introduction to Factorial Program in JavaScript

Factorial is a significant operation available in mathematics. It can be used in various types of applications. It is often a requirement to calculate the factorial on browsers. It is very much possible that we can calculate the factorial of the number using the JavaScript. JavaScript will allow us to calculate the factorial of any number at runtime. There is no restriction on the size of the number. It is also necessary that we write efficient code to find out the factorial. In this factorial program in javaScript article, we will see how to find out the factorial of the given number using JavaScript.

### Factorial Logic in JavaScript

- The logic to find out the factorial is basically independent of the programming language used, but we will discuss the general logic of factorial and will then see how the same logic can be applied in JavaScript.
- Factorial of number is nothing but the same number is multiplied by all numbers less than that number up to 1 and Factorial is denoted by ! symbol.
- The simplest way or logic to calculate the factorial program is by traversing each number one by one and multiplying it to the initial result variable. i.e. to find out the factorial of 5 we will traverse from 5 to 1 and go on multiplying each number with the initial result. In JavaScript, we can either traverse from 5 to 1 or from 1 to 5. In both cases, we will be using looping statements to implement the code. The initial result variable may contain any value except zero or negative value otherwise the result will be wrong. Note that the factorial is always calculated for positive numbers.
- There is another way or logic to calculate the factorial which is considered an efficient method. In this logic, factorial is calculated by using the recursion technique. The same function is called recursively until we met a certain final condition. In JavaScript, we can write a function that will call itself recursively until it reaches its limit condition and give the final result as factorial.

### How to Find the Factorial Program in JavaScript?

Let’s see examples of finding out the factorial in JavaScript using the methods we discussed above.

#### Example #1

Before we write down the actual JavaScript code we should have something like the webpage to display and interact with. We will design a simple HTML webpage that will have an input box as the number and will display the factorial of the entered number on the same page.

**Code:**

```
<!DOCTYPE html>
<html>
<head>
<title> Factorial of Number </title>
</head>
<body>
<h1> Factorial of Number using JavaScript </h1>
<h2> Enter the valid number...! </h2>
<input type = "number" min = "1" id = "num">
<button type = "button" onclick = "submit()" > Submit </button>
<h3> Factorial: <span id = "answer"> </span> </h3>
<script>
// This function will get value from the user and pass it to Factorial function
function submit()
{
var num = parseInt( document.getElementById("num").value);
if( isNaN(num) )
{
alert( " Enter valid number " );
return;
}
document.getElementById("answer").innerHTML = num;
}
</script>
</body>
</html>
```

**Output:**

So we have a simple page with an input box and submit button, upon submitting or on click the function submit() will be called. The function will parse the entered number and display it on the same page.

Let’s modify the same function to calculate the factorial of a number. The submit() function will parse the entered number and will pass it to another function calcFact() which will actually calculate the factorial of a number.

#### Example #2

By traversing the number in the forward direction starting from 1.

**Code:**

```
<!DOCTYPE html>
<html>
<head>
<title> Factorial of Number </title>
</head>
<body>
<h1> Factorial of Number using JavaScript </h1>
<h2> Enter the valid number...! </h2>
<input type = "number" min="1" id = "num">
<button type = "button" onclick = "submit()"> Submit </button>
<h3> Factorial: <span id = "answer"> </span> </h3>
<script>
// This function will get value from user and pass it to Factorial function
function submit()
{
var num = parseInt( document.getElementById("num").value);
if( isNaN(num) )
{
alert(" Enter valid number ");
return;
}
var factorial = calcFact(num);
document.getElementById("answer").innerHTML = factorial;
}
function calcFact( num )
{
var i;
var fact = 1;
for( i = 1; i <= num; i++ )
{
fact = fact * i;
}
return fact;
}
</script>
</body>
</html>
```

The function calcFact() will calculate the factorial by traversing the number from 1 to number itself. The function receives an argument as num which is then entered number by the user and performs the operation on it. The variable fact is initialized to 1 and then it is multiplied with all the numbers between 1 and entered a number.

**Output:**

#### Example #3

The same function calcFact() can be written in this way, By traversing the number in reverse direction starting from number itself to 1.

**Code:**

```
function calcFact( num )
{
var i;
var fact = 1;
for( i = num; i >= 1; i-- )
{
fact = fact * i;
}
return fact;
}
```

**Output:**

Here the variable i is initialized to number itself and is traversed by performing i–operation i.e. in reverse direction. This function will give the same result similar to the previous one.

#### Example #4

By using the recursive function.

**Code:**

```
function calcFact( num )
{
if(num == 1)
{
return num;
}
else
{
return num * calcFact( num-1 );
}
}
```

The function calFact() will call itself until it reaches to value 1, but while passing the value it will pass it decreasing by 1 every time. It will go on multiplying with the original result till it reaches value 1. The final multiplying result will be the factorial of a number itself.

** ****Output:**

### Conclusion

So, Factorial being an important operation in mathematics, it is easy to implement it in the JavaScript. JavaScript allows us to perform dynamic operations such as getting the number and performing operations on it at runtime. We have seen the different ways by which we can calculate the factorial in JavaScript.

### Recommended Articles

This is a guide to Factorial Program in JavaScript. Here we discuss the logic and various methods to find the factorial program in javascript with its code implementation. You may also look at the following articles to learn more –