JavaScript Program to Find the Factorial of a Number

Updated on September 30, 2024
Find the Factorial of a Number header image

Introduction

Finding the factorial of a number is a classic algorithmic problem often encountered in computer science education. Factorial, usually denoted as n!, is the product of all positive integers less than or equal to n. It's fundamental in many areas of mathematics, including combinatorics, algebra, and calculus.

In this article, you will learn how to implement a function to find the factorial of a number in JavaScript. Explore different methods including an iterative approach, a recursive solution, and using modern JavaScript features for more concise code. These examples will equip you with various ways to solve the problem efficiently.

Iterative Method to Calculate Factorial

Implement an Iterative Solution

  1. Initialize a variable to hold the factorial result, usually starting with the value 1.

  2. Use a for loop to multiply the result by each integer up to and including n.

    javascript
    function factorial(n) {
        let result = 1;
        for (let i = 1; i <= n; i++) {
            result *= i;
        }
        return result;
    }
    console.log(factorial(5));  // Output: 120
    

    This code uses a loop to progressively multiply the result by each number from 1 to n. When n is 5, the loop calculates 1 * 2 * 3 * 4 * 5, which equals 120.

Recursive Method to Calculate Factorial

Implement a Recursive Solution

  1. Realize that the factorial of n can be defined recursively as n * factorial(n-1), with the base case being factorial(0) = 1.

  2. Define the function recursively based on this relationship.

    javascript
    function factorial(n) {
        if (n === 0) {
            return 1;  // Base case
        }
        return n * factorial(n - 1);
    }
    console.log(factorial(5));  // Output: 120
    

    The recursive function calls itself with n - 1 until reaching the base case where n equals 0. Then it returns 1 and starts returning and multiplying the results back up the call stack.

Modern JavaScript Techniques

Using ES6 Features

  1. Consider employing arrow functions for more succinct code.

  2. Implement recursion with a ternary operator to compress the solution further.

    javascript
    const factorial = n => n === 0 ? 1 : n * factorial(n - 1);
    console.log(factorial(5));  // Output: 120
    

    This arrow function defines factorial more concisely using a ternary operator (?:) to manage the recursive calls and the base case. It is a compact version of the earlier recursive method.

Conclusion

Factorials are an interesting mathematical concept with numerous programming applications. By understanding how to calculate them using iterative and recursive methods, as well as taking advantage of modern JavaScript syntax, you significantly enhance your coding skills in JavaScript. Experiment with these methods to deepen your understanding of both recursion and iterative looping in solving real-world computing problems.