function fact(n) {
if (n > 1) {
return n * fact(n-1);
} else {
return 1;
}
}
console.log(fact(5));
Using ternary operator we replace the above code in a single line of code as below
function fact(n) {
return (n != 1) ? n * fact(n - 1) : 1;
}
console.log(fact(5));