Recursion in Javascript is a programming technique in which a function calls itself repeatedly until a specific condition is met. The function that calls itself repeatedly is called a recursive function. A recursive function consists of two cases: base case and recursive case.
Base case is the condition that stops the recursion, while the recursive case is the condition that allows the function to call itself again.
Here is an example of how to create a recursive function in Javascript:
index.tsx229 chars13 lines
In the example above, the recursive function recursiveFunction
takes an argument number
. If number
is equal to zero, the function will return 1, which is the base case. If number
is not equal to zero, the function calls itself with the argument number-1
, which is the recursive case.
When the function calls itself with the argument number-1
, it will continue to call itself until number
becomes zero. Once number
becomes zero, the function will start returning the result of each multiplication, finally resulting in the output of 120.
gistlibby LogSnag