Factorial of a number using JavaScript

Given a positive integer n and the task is to find the factorial of that number with the help of javaScript.
factorial of number

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!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>

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!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>

chevron_right


Output:

  • Before clicking on the button:
  • After clicking on the button:


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.