Recursive functions are those which, upon execution, evoke themselves . When there is a functionality which has to be repeated unknown number of times, using recursion is a good technique. Applications like repeated multiplication in calculating power, factorial, addition in counter, filling a color in a grid …etc use recursion.

In recursive functions, variables of the functions are pushed to a stack in each run. The variable of the first run is at bottom of the stack, the next variable is placed on top of that… and so on. When exit condition is reached, the values are popped and calculation is done.

Adventages of recursive functions

This way of coding helps "cleaner" coding.

Disadvantages of recursive functions

The stack depth in JavaScript is a browser specific. When a function is recursive such that stack memory is out of range, RangeError is thrown.

Example of a recursive function that calculates the power of a number

 

›› go to examples ››