Recursion can be a tricky topic for some people to get their head around, but once you get it, you get it. In this article, I’ll give a brief synopsis of what recursion is, how it works, and why it matters.

When I first learned about the concept of recursion, I struggled with it. I thought: “It was hard enough for me to learn the in’s and out’s of writing functions, now you’re asking me to write functions INSIDE of themselves?” This was a hard concept for me to get my head around but, like many other things in software engineering, once it clicked, it clicked.

Before we get into explanations and examples, let’s briefly discuss what recursion is and why you should care.

Recursion can be a tricky topic for some people to get their head around, but once you get it, you get it.
What exactly IS recursion?
Recursion is, in essence, a process that calls itself. In this article, we will explore this concept in the context of functions in JavaScript.

There are a few different reasons you’d call functions in this way. Sometimes it makes things easier, sometimes it makes things unnecessarily complicated, but in the situations where it makes things easier, it’s very important to know how to implement.

Let’s quickly go over some reasons that recursion is important, and the contexts in which you might find it useful.

Why does recursion matter?
Recursion is important for multiple reasons:

DOM traversal algorithms
The DOM can be seen as a tree-like structure. This tree having many branches, with each branch having many branches themselves, and on and on. (Think of a div within a div within a div…). Traversing through complex objects such as this can be made much easier/faster when recursive functions are used.

#recursion #software-development #big-o-notation #programming #coding

Tackling the Tricky Concept of Recursion
1.20 GEEK