Calculating the factorial of a number

For our first example of recursion, let’s take a look at how to compute the factorial of a number. A factorial of a number, n, is defined by n! and it is the result of multiplying the numbers from 1 to n.

The factorial of 5 is represented by 5! and it is equal to 5 * 4 * 3 * 2 * 1, resulting in 120.

Get Learning JavaScript Data Structures and Algorithms - Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.