The factorial of a provided integern is the product of all positive integers less than or equal to n.

For example: 4! = 1 * 2 * 3 * 4 = 24

As you can see below, the factorial of a number n  is calculated as the product of the factorial of the previous number with the numbern:

4! = 3! * 4 = (4 – 1)! * 4

3! = 2! * 3 = (3 – 1)! * 3

2! = 1! * 2 = (2 – 1)! * 2

function factorial(num) {
  if (num <= 1) {
    return 1;
  }
  return num * factorial(num - 1);
}

factorial(5);