Check out a free preview of the full Data Structures and Algorithms in JavaScript course:
The "Why Recursion?" Lesson is part of the full, Data Structures and Algorithms in JavaScript course featured in this preview video. Here's what you'd learn in this lesson:

Recursion occurs when a function calls itself. Bianca makes a case for recursion and talks about why understanding the core concepts will make it easier to understand other use cases like recursive algorithms or recursive data structures.

Get Unlimited Access Now

Transcript from the "Why Recursion?" Lesson

[00:00:00]
>> Bianca Gandolfo: A lot of people go through their life using recursion based on just memorizing a pattern of syntax. But we're gonna explore a little more in depth. Yeah. Cool.
>> Bianca Gandolfo: So what is recursion? So a lot of us have used it, so if I ask the question what is it, perhaps we have an answer somewhere.

[00:00:20]
>> Speaker 2: When a method calls itself.
>> Bianca Gandolfo: Yeah. Exactly. You're reading my slide. Look at that. You like it? Call me. Call me anytime. I just love it. This is how I get through this, just like, make my own silly jokes. So recursion is simply when a function calls itself, however, it doesn't stop there.

[00:00:40] No.
>> Speaker 2: It will never stop.
>> Bianca Gandolfo: No, this one will never actually stop, it'll infinitely be calling you like a creepy stalker until it gets a stack overflow. Cool, all right, so recursion defined a function that calls itself. That doesn't really mean much to us though. You know?

[00:01:00] Because recursion is just a way of approaching a problem. It's a way to take a procedure that's repeated over and over again. It's like a loop. A lot of the things that we use recursion for, you can simply do with a loop. Most of the time until you get, well you can actually always do it but it's a little more complicated.

[00:01:22] You can always do it with a loop and in, like, another data structure. A recursion with loop you can loop and do some practice in the exercises translating loops while loops for loops and two recursion. Until we get to the point we're like, wait, now it gets really hard to make that translation.

[00:01:42] Yeah? All right.
>> Bianca Gandolfo: Any questions so far?
>> Bianca Gandolfo: Okay. So right now we are going to talk about recursive functions. On tomorrow we are going to talk about recursive algorithms. And then when we get to trees and graphs we will talk about recursive data structures.
>> Bianca Gandolfo: Here we go.

[00:02:16]
>> Bianca Gandolfo: Why? Elegant solutions that keeps your code DRY: Don't Repeat Yourself. As opposed to WET: We Enjoy Typing. It's expected Computer Science knowledge. You're going to see these on interviews a lot. It's useful, especially for specific data structures. You feel really cool after writing like a really elegant recursive solution.

[00:02:42] It's like this much, this many lines of code and then it does a lot of cool stuff, yeah. This is exciting. It's difficult for people to tackle at first, so if this is your first time, it's totally cool. Slow me down. Ask me lots of questions. Happy to do that.

[00:03:03] All right, everybody ready? We're like at the top of the rollercoaster like choo, choo, choo. We're about to go into the recursion.