DEV Community

Cover image for How create a recursive function using Javascript
The Huferr
The Huferr

Posted on

How create a recursive function using Javascript

Hey, it’s Huferr.

Today, you’re gonna learn what is a recursive function and how to create one using Javascript.

🔎 What does recursion mean?

“In computer science, recursion is a method of solving a problem where the solution depends on solutions to smaller instances of the same problem. Recursion solves such problems by using functions that call themselves from within their own code.” - Wikipedia

Well, let's make it simpler to understand.

Basically, it means that you’re going call the function that you’re creating inside the function itself, at the same time of its creation.

To declare a recursive function, you need to use the function inside its own block of code:

const func = () => {
  func() // Whaaaat???
}
Enter fullscreen mode Exit fullscreen mode

⚠️ WARNING: You need to be careful, because you can easily create an infinite loop.

⚙️ Coding

Time to practice!

Let’s create a simple recursive function called countDown.

It will be responsible for displaying a count down in the console, starting from a given number.

Let’s set its argument as fromNumber. Then, console.log() it.

const countDown = (fromNumber) => {
  console.log(fromNumber)
} 
Enter fullscreen mode Exit fullscreen mode

Now, we need to decrease that number. So we can console.log() it again with a smaller one.

To achieve that, let’s create a constant called nextNumber, which has fromNumber, decreased by 1.

const countDown = (fromNumber) => {
  console.log(fromNumber)

  const nextNumber = fromNumber - 1
} 
Enter fullscreen mode Exit fullscreen mode

Great, now we’re going to create a simple condition: If nextNumber is greater than 0, let’s keep decreasing and displaying it in the console.

To do that, we can simply call this exact function (countDown) inside itself, passing nextNumber as its parameter.

And here we create the recursion:

const countDown = (fromNumber) => {
  console.log(fromNumber)

  const nextNumber = fromNumber - 1

  if (nextNumber > 0) {
    countDown(nextNumber)
  }
} 
Enter fullscreen mode Exit fullscreen mode

That’s it! You’ve created your first recursive function.

To test it, Let’s call the function, passing the number 5 as the argument and reload the console.

Great, here are the results:

Results

You can see that we have a controller for the Loop (or recursion), which is the condition:

if (nextNumber > 0) {
  countDown(nextNumber)
}
Enter fullscreen mode Exit fullscreen mode

Here, we’re telling the function to only call itself when nextNumber is greater than 0. Other than that, It won’t be called.

If we remove that condition, we will be creating an infinite loop. A situation where your function continues calling itself without stopping.

🧠 Overview

  • Recursive functions are those that call themselves within their own block of code;

  • Need to be careful not to create infinite loops (function being called without stopping. Always add a validation;

Thanks for reading this article.

-- The Huferr!

Follow me on:

📹 Youtube => https://www.youtube.com/@thehuferr
🧵 X/Twitter => https://twitter.com/the_huferr

Top comments (0)