DEV Community

Discussion on: Explain Recursion Like I'm Five

socratesdz profile image
Sócrates Díaz • Edited on

Do you know Matryoshka dolls?


When you open a doll, you find another doll inside, and when you open that one, there's another one inside. The act of doing this is called recursion. Let's write code for that.

function openDoll(doll) {
    if(doll.isEmpty()) {
        return doll;
Enter fullscreen mode Exit fullscreen mode

Recursion is a way of doing an operation over a set of values, where each value is related to the previous one, without iterating or using loops.