Factorial of a number using JavaScript - GeeksforGeeks

11 Oct, 2019
Given a positive integer n and the task is to find the factorial of that number with the help of javaScript.
Examples:
Input : 4
Output : 24

Input : 5
Output : 120

Approach 1: Iterative Method In this approach, we are using a for loop to iterate over the sequence of numbers and get the factorial.
Example:
<!DOCTYPE HTML> <html>   <head>     <title>         Factorial of a number using JavaScript    </title></head>   <body style = "text-align:center;">           <h1 style = "color:green;" >         GeeksForGeeks     </h1>          <p id = "GFG_UP" style =        "font-size: 15px; font-weight: bold;">    </p>              <button onclick = "GFG_Fun()">        Click Here    </button>          <p id = "GFG_DOWN" style =        "color:green; font-size: 20px; font-weight: bold;">    </p>              <script>        var up = document.getElementById('GFG_UP');         var down = document.getElementById('GFG_DOWN');         var n = 5;                  up.innerHTML = "Click on the button to calculate"                + " the factorial of n.<br>n = " + n;                  function Factorial(n) {            var ans=1;                          for (var i = 2; i <= n; i++)                ans = ans * i;            return ans;        }                  function GFG_Fun() {             down.innerHTML = Factorial(n);        }     </script> </body>   </html>
Output:
Before clicking on the button:
After clicking on the button:
Approach 2: Recursive Method: In this approach, we are calling the same function again and again to get the factorial of a number.
Example:
<!DOCTYPE HTML> <html>   <head>     <title>         Factorial of a number        using JavaScript    </title></head>   <body style = "text-align:center;">           <h1 style = "color:green;" >         GeeksForGeeks     </h1>          <p id = "GFG_UP" style =         "font-size: 15px; font-weight: bold;">    </p>          <button onclick = "GFG_Fun()">        Click Here    </button>          <p id = "GFG_DOWN" style =         "color:green; font-size: 20px; font-weight: bold;">    </p>          <script>        var up = document.getElementById('GFG_UP');         var down = document.getElementById('GFG_DOWN');         var n = 10;                  up.innerHTML = "Click on the button to calculate"                + " the factorial of n.<br>n = " + n;                  function Factorial(n) {            if (n === 0) {                 return 1;             }            else {                 return n * Factorial( n - 1 );             }        }                  function GFG_Fun() {             down.innerHTML = Factorial(n);        }     </script> </body>   </html>
Output:
Before clicking on the button:
After clicking on the button:
JavaScript
Web Technologies
Web technologies Questions
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Remove elements from a JavaScript Array
Convert a string to an integer in JavaScript
Difference between var, let and const keywords in JavaScript
Differences between Functional Components and Class Components in React
How to append HTML code to a div using JavaScript ?
Remove elements from a JavaScript Array
Installation of Node.js on Linux
Convert a string to an integer in JavaScript
How to fetch data from an API in ReactJS ?
Difference between var, let and const keywords in JavaScript