Intro to JavaScript Recursion  
Tags: javascript, beginners, codenewbie, webdev
Published: 2022-02-01 11:27:27

While working on projects I found recursion challenging. Perhaps this is due to the fact that many resources explain it using algorithmic examples (Fibonacci, linked-lists) and this made it quite difficult to understand. First of all, I should explain what Fibonacci and Linked lists are. 

## What is Fibonacci?
The Fibonacci Sequence is the series of numbers:

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ...

The next number is found by adding up the two numbers before it:

- the 2 is found by adding the two numbers before it (1+1),

- the 3 is found by adding the two numbers before it (1+2),

- the 5 is (2+3),

- and so on.

## What is a Linked-list?
While a linked list is a linear data structure, in which the elements are not stored at contiguous memory locations. The elements in a linked list are linked using pointers. These two keep going as long as there is a call. The numbers in a Fibonacci sequence would keep adding up as long as there is a set of numbers to add up, this same logic came be said applies to linked list and recursion which is why I'm using them as pointers. I might talk about Linked list in another post but I'd like us to focus on recursion today.

With the help of one basic example, this piece should be able to explain things clearly. When a function calls itself until it is stopped, this is known as recursion. If no one intervenes, it will continue to recurse (call itself) indefinitely.

The recursive function has the following syntax:

![Recursion function](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fndhc3l2k8k6jn0wi4r2.png)

The `recurse()` method is a recursive function in this case. Within the function, it is calling itself. The function is written in the ES6 Syntax. If you have no knowledge about JavaScript ES6 Syntax, you should check these resources:

- [JavaScript Tutorial](https://www.javascripttutorial.net/es6/) 

- [Tutorials Point](https://www.tutorialspoint.com/es6/es6_syntax.htm)

- [W3Schools](https://www.w3schools.com/js/js_es6.asp)

A condition must exist for a recursive function to stop calling itself. If not, the function will be called indefinitely.
The function stops calling itself after the condition is met. This is referred to as the base condition.
You can use an if...else statement (or a similar approach) to prevent infinite recursion by having one branch make the recursive call while the other does not.

So, this is how it appears in general.


![Recurse condition](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9nrovn2o03xorq8bvl6c.png)
This is referred to as the "base case".
It's the same concept as the logic that stops loops in JavaScript. Whatever strategy you choose, keep in mind that it will have to come to an end at some point.

An example of a recursive function:

## Count to Ten

![Base case](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eiu7sw72q1q6tqjrs1il.png)

## Output
![Output](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6khbi09hukdy7deo2t51.png)
When invoking a function in the above application, the user gives a number as an argument.

The number value is increased by 1 in each iteration, and the method `countToTen()` is called until the number is positive. The base condition is `num < 10`.

The base condition is satisfied when the number hits 10, and the function is no longer invoked.

## Summary

- When a function calls itself until it is stopped, this is known as recursion.

- It can be used instead of a loop.

- If no one intervenes, it will continue to recurse indefinitely, crashing your software.

- A condition that stops the recursion is known as a base case. Remember to include them!

## Thanks for reading
For more content about recursion like this, check out [freeCodeCamp](https://www.freecodecamp.org/news/what-is-recursion-in-javascript/) and [JavaScript.info](https://javascript.info/recursion).

Until next time.