- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Function to compute factorial of a number in JavaScript

In this article, the given task is to get the factorial of a number in JavaScript.

### What is the factorial of a number?

The multiplication of all positive integers smaller than or equal to n gives the factorial of a non-negative integer.

For example, assume the non-negative integer is 4 and the factorial of 4 will be 4*3*2*1 which is equal to 24. The symbol of factorial is represented by "!". so it will be (4!).

The value of 0! Will be 1 as 0 is not a positive integer.

### The mathematical formula of factorial

**("n!")** This is how the factorial of a number is represented. it is the product of all positive integers which are less than or equal to n.

n! = n*(n-1)*(n-2)*……3*2*1

## Using for loop

We can calculate the factorial of a number by using for loop.

### Algorithm

These are the steps to get the factorial of a given number by using for loop.

create a variable

**(result)**with value 1.Call the function

**fact(num)**.If the

**num**is less than 0, will return -1.If the

**num**= 0, will return 1.If the

**num**> 1, with each iteration it will decrease the value by 1 and multiply by the**result**.The factorial result will be stored in the

**result**variable.

### Example

Following is the program to get the factorial of a number by using the above for loop algorithm.

<!DOCTYPE html> <html> <head> <title>Factorial of a number</title> <button onClick = "func()"> Click to get factorial </button> <p id="para"> </para> <script> function func() { function fact(num) { if (num < 0){ return -1; } else if(num == 0){ return 1; } else { let result = 1; for(var i = num; i > 1; i--){ result *= i; }; return result; } }; const num = 4; document.getElementById("para").innerHTML = fact(num); }; </script> </head> </html>

As we can see in the output, it calculated the factorial of a number with the help of for loop by iterating the input number and decreasing it for every iteration.

## Using recursion

We can get the factorial of a number by recursion.

### Algorithm

Below are the following steps to achieve factorial of number by recursion.

Get the number from

**num**.Call the function

**factorial(num)**.If

**num**is less than 0, then return -1.Else if

**num = 0**return 1.Else, return (num * factorial(num - 1))

### Example

Following is the example of getting the factorial of a number by using the above recursive algorithm −

<!DOCTYPE html> <html> <title>Factorial of a number</title> <head> <button onClick = "func()">Factorial</button> <p id="para"> </para> <script> function func(){ function factorial(num) { if (num < 0) { return -1; } else if (num == 0){ return 1; } else { return num * factorial(num - 1); } } let num = 4; document.getElementById("para").innerHTML = factorial(num); }; </script> </head> </html>

On executing the above program a button named Factorial is displayed. If you click on this button The factorial of the input number (4) is calculated and the result is printed.

## Using While loop

We can achieve the factorial of a number by using while loop.

### Algorithm

These are steps to calculate the factorial of a number by using while loop.

create variable

**res**.Call the function

**fact(num)**.If the

**num**is less than 0, will return -1.If the

**num = 0,**will return 1.If the

**num > 1,**with each iteration it will decrease the value by 1 and multiply by the**result**.The factorial result will be stored in the

**result**variable.

### Example

Following is the example of getting the factorial of a number by using a while loop −

<!DOCTYPE html> <html> <head> <title>Factorial of a number</title> <button onClick = "func()">Click me! </button> <p id = "para"></para> <script> function func(){ function fact(num) { var res = num; if (num === 0){ return 1; } else if (num === 1){ return 1; } else{ while (num > 1) { num--; res *= num; } } return res; }; const num = 4; document.getElementById("para").innerHTML = fact(num); }; </script> </head> </html>

As we can see from the output, the while loop was used to calculate the factorial of a given number by iterating the input number and decreasing it each time.

- Related Articles
- Returning number of digits in factorial of a number in JavaScript
- Calculating a number from its factorial in JavaScript
- Factorial of a large number
- Comparing the performance of recursive and looped factorial function in JavaScript
- How to make a function that returns the factorial of each integer in an array JavaScript
- Java Program to Find Factorial of a number
- Swift Program to Find Factorial of a number
- Kotlin Program to Find Factorial of a number
- How to write the factorial function with reduce and range in JavaScript?
- How to find the Factorial of a number in Golang?
- 8085 program to find the factorial of a number
- 8086 program to find the factorial of a number
- Python program to find factorial of a large number
- First digit in factorial of a number in C++
- Python Program for factorial of a number