What is Recursion?

The process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called a recursive function. In the recursive program, the solution to the base case is provided and the solution to the bigger problem is expressed in terms of smaller problems. 

Examples:

Input : 5
Output : 1 2 3 4 5

Input : 10
Output : 1 2 3 4 5 6 7 8 9 10

JavaScript Program to Print 1 to N using Recursion

In this article, we will see how to print 1 to N using Recursion in JavaScript.

Similar Reads

What is Recursion?

The process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called a recursive function. In the recursive program, the solution to the base case is provided and the solution to the bigger problem is expressed in terms of smaller problems....

Approach:

We create a function that takes two arguments: “num” which is the number up to which we have to print and “currentValue” which prints the current number. Check for the base case. Here it is currentValue > num. If the base condition is satisfied, then it returns and ends the recursion. If the base condition is not satisfied, print currentValue and call the function recursively by increasing the currentValue by 1 and num, until the base condition satisfies....