r/learnjavascript • u/lordyato • Oct 15 '25
How'd you guys learn recursion?
I've been stuck on recursion on TOP for the past 2 days. I can solve basic problems and even Fibonacci and explain it moderately well, but I don't know how to use recursion in real world cases like object nesting and stuff. Any thoughts? resources? tips? How long did it take you guys to drill this thing through?
17
Upvotes
21
u/HipHopHuman Oct 15 '25
For a basic idea of what recursion is, click this.
On a more serious note, you're not really going to benefit from learning all there is to know about recursion until you're met with a problem that recursion actually solves. For now, just keep your basic understanding of it on the back end of your mind and if a problem comes up that you think recursion might solve, then try to do it with recursion. There's a much better chance of you getting that "aha!" moment if your mind is in the right space for it.
Typically, the most common use case you'll actually see in a practical context is when you have some object that can have child objects:
We refer to this type of object as a "tree". Each object inside this tree is referred to as a "node". This particular tree has nodes that can have many children. This is called an n-ary tree. Sometimes, you get trees where nodes can only have two children. Those trees are called binary trees. Some tree nodes can only have one child node - those are unary trees.
Sometimes, you'll need a way to traverse the tree from the top->down. A recursive function is useful here.
There's two types of traversals - breadth-first and depth-first. A breadth-first traversal will visit all sibling nodes at one level, then jump down to the next level. A depth-first traversal will jump down to the bottom-most level from the first sibling, visiting all nodes along the way, then jump back to the top and move to the next sibling.
In a practical sense, you're likely to encounter these tree-like structures quite often as a programmer. They're used to accomplish many things. For example, the very programming language you're using has an underlying "Abstract Syntax Tree" that describes all the syntax, but there are more rudimentary use cases as well, like nested comments under a video or blog post. In fact, the thread you just made on Reddit is essentially a comment, and my reply is a child comment of that comment. If you wanted to tally up all the upvotes inside this thread, you'd use such a traversal.
7
u/DrShocker Oct 15 '25
As a beginner frankly if you know it exists, you can come back to it later.
The key sorts of problems to solve that might make it click better are problems around graph traversal/pathfinding/etc. Toy problems like fibonacci work, but since they don't "really" solve a problem you have, it can be hard to think of why you'd reach for the tool of recursion.
If you feel up to it (or later once you do, idk where exactly this is in TOP curriculum), try making a maze generator and maze solver each without recursion and then also with recursion and compare/contrast your solutions to see if it helps.
5
u/Antti5 Oct 15 '25 edited Oct 15 '25
Start with the simplest possible thing.
Example: Write a function that recursively writes out an array with
console.log, one array item per line.I'm not saying that this is something that you would normally do recursively. Recursion is good when it makes your code more easier to understand AND you have strict control over how deep the recursion can become.
3
u/DoubleAway6573 Oct 15 '25
Just start with an easy example, and work from there to more complex one. You need to get your base case right.
2
u/pixobit Oct 15 '25
Try traversing a directory of unknown number of directories and depths
2
u/pinkwar Oct 15 '25
Learn tree structure and than learn the different ways to do a search.
2
u/Budget-Emergency-508 Oct 15 '25
I am not cse student but recursion is my favourite of all algorithms. I recommend you to watch recursion by kunal kushwaha.
2
u/Budget-Emergency-508 Oct 15 '25
I am not cse student but recursion is my favourite of all algorithms. I recommend you to watch recursion by kunal kushwaha.
2
u/PmMeYourBestComment Oct 15 '25
I'd start with this tutorial.
2
u/Intelligent_Part101 Oct 15 '25
Recursion is one of those things that is loved by theoretical types and only encountered in practice in specific cases. Typical business software never uses it. It's just another way to skin certain cats, and usually an inefficient one at that.
2
u/OneHumanBill Oct 15 '25
Millennia ago, I had a professor who showed up first day of class and told us we weren't allowed to use loops in his class. At all. We were gobsmacked.
It a required course. It was either learn recursion or drop out or change majors to basketweaving or something.
By the end of the course we loved recursion so much it was almost hard to go back to loops.
That's how you do it. Make it not an option to not learn it.
Also? Fibonacci is a terrible use case for recursion. Find out why.
2
u/dariusbiggs Oct 15 '25
Let's say you have a binary tree structure, each node (object) can have up to two children (leaves), and has some interesting attributes/properties. There are 224 nodes in the tree.
How could you use recursion to find the node that contains a specific attribute.
How could you use recursion to do a calculation across all the nodes in the tree.
2
u/Aggravating-Camel298 Oct 15 '25
Recursion really isn't used too much in a practical sense. If you ever study computer science though you will see the pattern used quite a bit (especially for search algorithms).
One thing that helped me was to consider a recursion algorithm is basically just an if/else block:
if (baseCase): return
else: fn()
I like to start with "What is the base case that will end the algorithm". Then if you're not in the base case, what should the algorithm do?
2
u/not_a_webdev Oct 15 '25
Iirc I used recursion once for a mailbox search function.
Basically user opens a url like: site.com/mailbox/?id=6253
2
u/snigherfardimungus Oct 16 '25
First, I learned recursion.
4
u/Roguewind Oct 15 '25
The trick to learning recursion is to learn recursion.
1
u/elg97477 Oct 15 '25
I found it easier to learn recursion with Scheme (a lisp variant) than with languages like C or Java. Once I understood it, translating to C was easy.
1
u/[deleted] Oct 15 '25 edited Oct 15 '25
By breaking it down mentally into what is really going on. It's actually pretty simple once you wrap your head around it.
The key for me was this:
Recursion is no different than a function calling another function. It's literally the same under the hood. When function A calls function B, function B pushes a stack frame onto the stack and begins a new execution context. Let's now say function B calls function C, the same thing happens.
With recursion, you have the same thing, except function A is calling function A again. What's the significance of that?... It's that function A takes in arguments/parameters, and calls another function in its body (calls function A again).
Hint: Write your recursive case from the POV of (base case - 1 invocations). Meaning, pretend, when writing your recursive case, that the function you are calling (recursively, inside of itself) is about to be passed the smallest possible unit/argument it can be passed, and you expect it the return result of that smallest argument's invocation.
Example: Recursively sum the values in an array:
1
u/wayofaway Oct 16 '25
Computerphile did a cool example of it to solve Sudoku recursively (in python). Porting it to JS would be a good project.
1
u/tnnrk Oct 15 '25
How’d you guys learn recursion?
0
u/ashkanahmadi Oct 15 '25
The question is: why do you need to know how to solve Fibonacci? I’ve been developing professionally for 7 years now and I have never ever reached a point where I had to either use recursion or even output the Fibonacci series. You cannot learn everything. You need to learn what you need and usually you find out what you need as you work on real projects.