JavaScript Example: Recursion

By Xah Lee. Date:

This page shows a example defining a factorial function, using recursion.

function ff(n) {
    if (n < 0)  { return -1; }

    if (n == 0) {
        return 1;
    }
    else {
        return (n * ff(n - 1));
    }
}

document.getElementById("id19564").onclick = function(){ alert(ff(5));};

to see result.

Like what you read? Buy JavaScript in Depth
or, buy a new keyboard, see Keyboard Reviews.