# What is Recursion?

In this tutorial we will learn What is Recursion and how to use it with some examples.

Recursion is a widely used phenomenon in computer science used to solve complex problems by breaking them down into simpler ones. It is the process by which a function calls itself directly or indirectly. The corresponding function is called a recursive function.

## What is a base condition in recursion?

The role of the base condition is to stop the recursive function from executing endlessly once a prespecified condition is met. In the below example we are iterating over an array. The base condition is if the length of the array is 0 -> (n.length == 0) then we will not start the function anyway and add a return statement . Where n is the length of an array.

if(i == n.length){
return;
}

## Looping Over An Array Using Recursion

In the below example we are going to get elements from an array one by one using a recursive function. Follow the below steps to iterate over an array:

### Explanation:

• Create a function and take the array in parameter.
• The base condition is array.length == 0 or it can be as if the value of i is equal to the length of the array, then we will stop the function by adding a return statement.
• Declare a variable i =0;
• In the recursive function first get the first element from the array by passing the i.
• Then increase the i value by i++;
• Then call the recursive function it will call itself until the value of i is not equal to the array’s length.
``````
// Looping over array using recursion
var n = [10,22, 33,112,12];
var i=0;
call(n)

function call(n){
if(i == n.length){
return;
}
console.log(n[i]);
i++
call(n)

}
``````

10
22
33
112
12

## Print Counts Using Recursion

Printing counts up to the given number in JavaScript using Recursion. Follow the below algorithm.

``````
var n = 10;
call(n)
function call(n){
if(n == 0){
return;
}
console.log(n);
n--
call(n)

}
``````

10
9
8
7
6
5
4
3
2
1

Thank you for reaching out to this tutorial, you can comment in the comment section below for any query and doubts. You can also post your content in FlutterTPoint. You can read more about It From Wikipedia.