DEV Community

Cover image for Recursion in JavaScript
Anas Nabil
Anas Nabil

Posted on • Edited on

14 7

Recursion in JavaScript

What is Recursion?

A recursive function is a function that calls itself until it doesn’t. And this technique is called recursion.


Syntax

const recurse = () => {
    recurse();
}

recurse();
Enter fullscreen mode Exit fullscreen mode

This recursive function will keep calling itself forever, So it needs a little more touches

const recurse = () => {
  if (condition) {
    recurse();
  }
  // stop calling recurse()
};

recurse();
Enter fullscreen mode Exit fullscreen mode

This function will continue calling itself as it meets the condition, else will stop running.


Examples

1- Simple Example

const countDown = (start, end) => {
  console.log(start);
  if (start > end) {
    countDown(start - 1, end);
  }
};

countDown(19, 7); // 19 18 17 16
Enter fullscreen mode Exit fullscreen mode

Behind the scenes

  • countDown(19, 7) prints 19 and calls countDown(18, 7)
  • countDown(18, 7) prints 18 and calls countDown(17, 7)
  • countDown (17, 7) prints 17 and calls countDown(16, 7)
  • countDown (16, 7) prints 16 and stop running.

2- Factorial

  • 0! = 1
  • 1! = 1
  • 2! = 2 * 1
  • 3! = 3 * 2 * 1
  • 4! = 4 * 3 * 2 * 1
  • 5! = 5 * 4 * 3 * 2 * 1
const factorial = (num) => (num < 0 ? -1 : num === 0 ? 1 : num * factorial(num - 1));

console.log(factorial(5)); // 120
Enter fullscreen mode Exit fullscreen mode

Behind the scenes

  • factorial(5) = 5 * factorial(4)
  • factorial(4) = 4 * factorial(3)
  • factorial(3) = 3 * factorial(2)
  • factorial(2) = 2 * factorial(1)
  • factorial(1) = 1 * factorial(0)
  • factorial(0) = 1

3- Fibonacci

A fibonacci sequence is written as:
0, 1, 1, 2, 3, 5, 8, 13, 21, ...

The Fibonacci sequence is the integer sequence where the first two terms are 0 and 1. After that, the next term is defined as the sum of the previous two terms. Hence, the nth term is the sum of (n-1)th term and (n-2)th term.

Here's a code that returns the fibonacci value at a given index using recursion

const fibonacci = (n) => (n < 2 ? n : fibonacci(n - 1) + fibonacci(n - 2));

console.log(fibonacci(5)); // 5
Enter fullscreen mode Exit fullscreen mode

Behind the scenes

  • fibonacci(5) = fibonacci(4) + fibonacci(3)
  • fibonacci(4) = fibonacci(3) + fibonacci(2)
  • fibonacci(3) = fibonacci(2) + fibonacci(1)
  • fibonacci(2) = fibonacci(1) + fibonacci(0)
  • fibonacci(1) = 1
  • fibonacci(0) = 0

Sentry blog image

How to reduce TTFB

In the past few years in the web dev world, we’ve seen a significant push towards rendering our websites on the server. Doing so is better for SEO and performs better on low-powered devices, but one thing we had to sacrifice is TTFB.

In this article, we’ll see how we can identify what makes our TTFB high so we can fix it.

Read more

Top comments (0)

The Most Contextual AI Development Assistant

Pieces.app image

Our centralized storage agent works on-device, unifying various developer tools to proactively capture and enrich useful materials, streamline collaboration, and solve complex problems through a contextual understanding of your unique workflow.

👥 Ideal for solo developers, teams, and cross-company projects

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay