Computer >> Computer tutorials >  >> Programming >> Javascript

Calculating factorial by recursion in JavaScript


We are required to write a JavaScript function that computes the Factorial of a number n by making use of recursive approach.

Example

The code for this will be −

const num = 9;
const recursiceFactorial = (num, res = 1) => {
   if(num){
      return recursiceFactorial(num-1, res * num);
   };
   return res;
};
console.log(recursiceFactorial(num));
console.log(recursiceFactorial(6));
console.log(recursiceFactorial(10));
console.log(recursiceFactorial(5));
console.log(recursiceFactorial(13));

Output

The output in the console −

362880
720
3628800
120
6227020800