 # Factorials In Javascript (With Recursion)

To learn JavaScipt, I’m redoing some problems I’ve done to learn Ruby. Previously, I wrote a Ruby method to find a factorial of a number both recursively and non-recursively. You can read the original post here.

This is how you solve the same problem with Javascript:

## Non-Recursively

```//prompt user to enter a number to calculate the factorial
var num = prompt(&quot;What number do you want to find the factorial of?&quot;)

var factorial = function(n) {
if(n == 0) {
return 1
} else {
product = 1;
for(i = 1; i &lt;= n; i++) {
product *= i;
}
return product;
}
}

console.log(factorial(num));

```

## Recursively

```//prompt user to enter a number to calculate the factorial
var num = prompt(&quot;What number do you want to find the factorial of?&quot;)

//recursive
var factorial = function(n) {
if(n == 0) {
return 1
} else {
return n * factorial(n - 1);
}
}

console.log(factorial(num));
```

Enjoy the article? Join over 20,000+ Swift developers and enthusiasts who get my weekly updates.