Recursion
During my (admittedly short) time learning JS, recursion was the first concept that really blew my mind. Up until that point, I’d been trying so hard to avoid creating loops without clear iteration points, and suddenly the idea that a block of code could… call itself?? Terrifying and exciting at the same time. But in practice, the idea isn’t actually that hard to understand or implement. It can get extremely complex depending on the data you’re working with, but it’s also one of the most fun types of functions to write, at least in my opinion.
One of our teachers explained recursion like stepping into a revolving door and doing something each time you go around. There’s no natural end until you stick a doorstop into the door, which forces it to stop and lets you exit. That analogy instantly clicked for me.
I like to start with a basic function and make sure that—at the return statement—it calls itself again, usually with slightly different parameters. That’s the “revolving door.” For example, let’s say we’re going to count down from a given number to zero and print each number to the console. That’s our “do something each time.” So I’ll start by adding a return case just to make sure the recursion is… well, recursing. The door is revolving.
function recursionCountdown(number) { // function declaration & parameter
return recursionCountdown(?number?) // we will worry about this later
};
We can build recursive functions to handle all sorts of situations inside the parameter we’re given. When we add these different situations, we call them cases—as in: “in this case, do this.” In the revolving-door analogy, this is your door hitting its threshold, checking for a doorstop. If there’s no doorstop, it keeps spinning.
So the next thing I like to do is add a placeholder case that returns the final value I’m looking for. This is our doorstop! I’ll have it print “No more positive numbers!” once we’ve counted all the way down. Since we’re calling the function again on the number, this has to happen when the number reaches zero—our doorstop.
function recursionCountdown(number) {
if (n === 0){
console.log('No more positive numbers!'); // this is my ‘return’ statement—
} // after looping, the code needs to exit here!
return recursionCountdown(?number?)
};
This also tells me that I need to call the function on the number minus one each time so we can print each descending value. Great! Now I know what my function parameter needs to be, and what goes outside the if statement—the printing of the number:
function recursionCountdown(number) {
if (n === 0){
console.log('No more positive numbers!');
}
console.log(n); // print the number
return recursionCountdown(number - 1); // then call the function on the next number
};
Okay great, now we’re getting somewhere!
The number enters the function, it checks if it’s zero, prints the number, then calls itself again with the number minus one. That next number enters the function, checks if it’s zero, prints the number, calls itself again (now the original number minus 2), and so on until we finally hit zero and stop revolving through the door.
When we run:
recursionCountdown(5);
We get:
5
4
3
2
1
No more positive numbers left!`
This works as intended! But I also want zero in that list so it feels more complete. To do that, I’ll change the === to <= so zero gets included:
function recursionCountdown(number) {
if (n <= 0){
console.log('No more positive numbers!');
}
console.log(n); // print the number
return recursionCountdown(number - 1);
};
recursionCountdown(5);
5
4
3
2
1
0
No more positive numbers left!
Perfect. Now we have a recursive function.
You can apply these same principles to all kinds of tasks: manipulating data, pushing values into other objects or arrays, slicing arrays as you “descend,” and so on. Your revolving door’s “do something,” “check for doorstop,” and “doorstop” can be anything you want—as long as they’re in the right order:
do something → check for doorstop → keep revolving… until you hit the doorstop.
In the end, recursion has honestly become one of my favorite things to write. There’s just something really cool about a function that can call itself and slowly work its way down to the answer. It can definitely get confusing or feel a little wild when things get more complicated, but that’s also what makes it so fun. Every recursive function feels like a tiny puzzle where you figure out what happens each “spin” around the door and where the doorstop goes.
For something that originally seemed scary, recursion turned out to be one of the most interesting and enjoyable tools I’ve learned so far.
Top comments (0)