Here's an example of a recursive function in JavaScript:
index.tsx147 chars12 lines
This function takes a number n
and counts down from it to 0, logging each number to the console. The if
statement serves as the base case, which is the point at which the function stops calling itself recursively. In this case, the base case is when n
reaches 0. The recursive case comes after the base case, and in this function, it involves calling countdown
with n - 1
. This is what makes the function call itself repeatedly until it reaches the base case.
gistlibby LogSnag