If you want a result from a recursive function, all code paths through the function must return something. Your code isn't returning anything in the num!=1 case. It should be returning the result of calling itself, e.g. (see the *** line):

var fact=5;
function calfact(num)
{
 if(num!=1)
  {
   fact=fact*(num-1);
   num=num-1;
   return calfact(num); // ***
  }
 else
  {
   return fact;
  }
}

Your function is using a global variable, which isn't a great idea as it means the funtion isn't self-contained; and isn't a true factorial function, because you're effectively using two inputs (fact โ€” the global  and num, the argument)

If you want a true factorial, you don't need a global variable, just work from the argument itself:

function factorial(num) {
    if (num < 0) {
        throw new Error("num must not be negative");
    }
    if (num <= 1) {
        // Both 1! and 0! are defined as 1
        return 1;
    }
    return num * factorial(num - 1);
}
console.log(factorial(5)); // 120

Or of course, more compactly:

function factorial(num) {
    if (num < 0) {
        throw new Error("num must not be negative");
    }
    return num <= 1 ? 1 : num * factorial(num - 1);
}

(More about 0!: https://en.wikipedia.org/wiki/Factorial)

Answer from T.J. Crowder on Stack Overflow
Top answer
1 of 3
3

If you want a result from a recursive function, all code paths through the function must return something. Your code isn't returning anything in the num!=1 case. It should be returning the result of calling itself, e.g. (see the *** line):

var fact=5;
function calfact(num)
{
 if(num!=1)
  {
   fact=fact*(num-1);
   num=num-1;
   return calfact(num); // ***
  }
 else
  {
   return fact;
  }
}

Your function is using a global variable, which isn't a great idea as it means the funtion isn't self-contained; and isn't a true factorial function, because you're effectively using two inputs (fact โ€” the global  and num, the argument)

If you want a true factorial, you don't need a global variable, just work from the argument itself:

function factorial(num) {
    if (num < 0) {
        throw new Error("num must not be negative");
    }
    if (num <= 1) {
        // Both 1! and 0! are defined as 1
        return 1;
    }
    return num * factorial(num - 1);
}
console.log(factorial(5)); // 120

Or of course, more compactly:

function factorial(num) {
    if (num < 0) {
        throw new Error("num must not be negative");
    }
    return num <= 1 ? 1 : num * factorial(num - 1);
}

(More about 0!: https://en.wikipedia.org/wiki/Factorial)

2 of 3
1
var fact=5;
function calfact(num){
   if(num!=1){
      fact=fact*(num-1);
      num=num-1;
      return calfact(num);//the missing thing
   }else{
      return fact;//why fact? i think it should be 1
   }
 }

By the way, your approach is maybe working, but really bad style.May do this:

function calfact(num){
  if(num!=1){
    return calfact(num-1)*num;
  }else{
    return 1;
 }
}

Or short:

calfact=num=>num==1?1:calfact(num-1)*num;
๐ŸŒ
W3Resource
w3resource.com โ€บ javascript-exercises โ€บ javascript-recursion-function-exercise-1.php
JavaScript recursion function: Calculate the factorial of a number - w3resource
February 28, 2025 - Write a JavaScript function that calculates the factorial of a number using tail recursion for optimization.
๐ŸŒ
Programiz
programiz.com โ€บ javascript โ€บ examples โ€บ factorial-recursion
JavaScript Program to Find Factorial of Number Using Recursion
To understand this example, you should have the knowledge of the following JavaScript programming topics: ... The factorial of a number is the product of all the numbers from 1 to that number. For example, factorial of 5 is equal to 1 * 2 * 3 * 4 * 5 = 120. ... The factorial of negative numbers ...
๐ŸŒ
Vultr Docs
docs.vultr.com โ€บ javascript โ€บ examples โ€บ find-factorial-of-number-using-recursion
JavaScript Program to Find Factorial of Number Using Recursion | Vultr Docs
November 6, 2024 - Learn that in the case of factorial calculation, the recursive relation is n! = n * (n-1)!, and the base case is typically 0! = 1. Define a JavaScript function named factorial that accepts one parameter n, the number whose factorial is required.
๐ŸŒ
Dillion's Blog
dillionmegida.com โ€บ p โ€บ factorial-with-recursion-in-js
How to find the factorial of a number using Recursion in JavaScript - Dillion's Blog
October 7, 2022 - As I mentioned at the beginning, the factorial of a number is the number multiplied by its precedents until the number 1. So, we can create a base case that lets the factorial function stop recursing when n is 1 like this:
๐ŸŒ
Stack Abuse
stackabuse.com โ€บ calculate-factorial-with-javascript-iterative-and-recursive
Calculate Factorial With JavaScript - Iterative and Recursive
March 23, 2023 - In this tutorial, we will learn how to calculate the factorial of an integer with JavaScript, using loops and recursion. We can calculate factorials using both the while loop and the for loop. We'll generally just need a counter for the loop's termination and the supplied number we're calculating a factorial for. ... function getFactorialForLoop(n) { let result = 1; if (n > 1) { for (let i = 1; i <= n; i++) { result = result * i; } return result; } else { return "n has to be positive"; } }
๐ŸŒ
JavaScript.info
javascript.info โ€บ tutorial โ€บ the javascript language โ€บ advanced working with functions โ€บ recursion and stack
Calculate factorial
n * factorial(n - 1) : 1; } alert( factorial(5) ); // 120 ยท The basis of recursion is the value 1. We can also make 0 the basis here, doesnโ€™t matter much, but gives one more recursive step: function factorial(n) { return n ?
๐ŸŒ
JavaScript.info
javascript.info โ€บ tutorial โ€บ the javascript language โ€บ advanced working with functions
Recursion and stack
In other words, the result of factorial(n) can be calculated as n multiplied by the result of factorial(n-1). And the call for n-1 can recursively descend lower, and lower, till 1. function factorial(n) { return (n != 1) ?
Find elsewhere
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ javascript โ€บ javascript-program-to-find-factorial-of-a-number-using-recursion
JavaScript Program to Find Factorial of a Number using Recursion - GeeksforGeeks
July 23, 2025 - In this approach the recursive function uses the ternary operator to check if the base case (num === 0) is met, returning 1, otherwise calls itself with (num - 1) and multiplying num with the result.
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ how-to-factorialize-a-number-in-javascript-9263c89a4b38
Three Ways to Factorialize a Number in JavaScript
March 16, 2016 - function factorialize(num) { // If the number is less than 0, reject it. if (num < 0) return -1; // If the number is 0, its factorial is 1. else if (num == 0) return 1; // Otherwise, call the recursive procedure again else { return (num * ...
๐ŸŒ
Ovexro
blog.ovexro.com โ€บ home โ€บ education & e-learning โ€บ understand recursion: a simple javascript factorial example
Understand Recursion: A Simple JavaScript Factorial Example โ€“ OVEX TECH
The factorial of 5 is found by multiplying 5 by 4, then by 3, then by 2, and finally by 1. This calculation results in 120. You can see a pattern here: the multiplication continues until we reach 1. This pattern of multiplying down to 1 is important.
Published ย  5 days ago
๐ŸŒ
Medium
medium.com โ€บ @roshan.waa โ€บ understanding-recursion-in-javascript-a-step-by-step-guide-with-factorial-example-and-how-it-works-aaa4ebbb4d0d
Understanding Recursion in JavaScript: A Step-by-Step Guide with Factorial Example and How it Works? | by roshan.waa | Medium
May 4, 2023 - In this example, weโ€™re calling the factorial function with an argument of 5. The function first checks if 5 is equal to 1, which is false, so it calls itself with 4 as the argument and multiplies the result by 5. This process continues recursively until the base case is reached when n equals 1.
๐ŸŒ
GoLinuxCloud
golinuxcloud.com โ€บ home โ€บ javascript โ€บ javascript recursion function to find factorial [solved]
JavaScript Recursion Function to Find Factorial [SOLVED] | GoLinuxCloud
January 17, 2023 - If n is not equal to 0 or 1, the function returns n multiplied by the factorial of n-1. This is the recursive case of the function, where it calls itself with n-1 as the argument, and uses the result of that call as part of the final calculation.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ factorial-recursion-in-javascript
Factorial recursion in JavaScript
September 14, 2020 - We are required to write a JavaScript function that computes the Factorial of a number n by making use of recursive approach. Here, we are finding the factorial recursion and creating a custom function recursiceFactorial() โˆ’
๐ŸŒ
DEV Community
dev.to โ€บ avinashrepo โ€บ 12-way-of-factorial-implementation-step-by-step-in-js-314n
12 way of factorial implementation step by step in js - DEV Community
February 4, 2024 - A concise recursive version using a ternary operator. Example usage: result2 = factorialTernary(4), outputs 24. While Loop Iterative Function (factorialWhileLoop):
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ recursion-in-javascript
What is Recursion in JavaScript?
April 2, 2025 - Recursion and loops work in similar ways. Every recursive function you write has an alternative solution with a loop. For example, you can create a function to find the factorial of a given number using both recursion and loops.
๐ŸŒ
Algorithms
alg24.com โ€บ en โ€บ factorial-in-javascript-recursive
Factorial in JavaScript - recursive - What is an algorithm?
If so, in line 8 we specify the result of the function as n multiplied by the value of the factorial function (the same factorial function we are currently defining) for n minus 1 (4). At this point, the factorial function calls itself. We call this a recursive function.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ calculating-factorial-by-recursion-in-javascript
Calculating factorial by recursion in JavaScript
Calculating factorial by recursion ... approach.ExampleThe code for this will be โˆ’const num = 9; const recursiceFactorial = (num, res = 1) => { if(num){ return recursiceFactorial(num-1, res * num); };...
๐ŸŒ
SitePoint
sitepoint.com โ€บ blog โ€บ javascript โ€บ recursion in functional javascript
Recursion in Functional JavaScript โ€” SitePoint
November 11, 2024 - For a factorial calculated this way, the terminal case comes when the number passed in is zero or negative (we could also test for negative values and return a different message, if we so desired). One problem with contemporary implementations of JavaScript is that they donโ€™t have a standard ...