What is Recursion? A recursive function explained with JavaScript code examples

Click for: original source

Recursion is a technique used to solve computer problems by creating a function that calls itself until your program achieves the desired result. By Nathan Sebhastian.

function randomUntilFive(result = 0, count = 0){
    if(result === 5){
        // base case is triggered
    }
    // recursively call the function
}

randomUntilFive();

This tutorial will help you to learn about recursion and how it compares to the more common loop:

  • What is recursion?
  • Why don’t you just use loop?
  • How to read a recursive function
  • How to write a recursive function

When reading a recursive function, you need to simulate a situation where the base case is immediately executed without executing the recursive call. Nice one!

[Read More]

Tags programming nodejs javascript app-development learning