Skip to content

Recursion

Arghun-dev edited this page Jul 24, 2021 · 8 revisions

What is Recursion ?

A process (a function in our case) which calls itself.

It's everywhere: JSON.parse, JSON.stringify

The Call Stack

  1. Any time a function is invoked it is placed (pushed) on top of the call stack.
  2. When Javascript sees the return keyword, or when the function ends, the compiler will remove (pop)

The essential parts of a recursive function => Base Case and Different input

Base Case

The condition when the recursion ends => this is the most important concept to understand.

CountDown.js

function CountDown(num) {
  // this is our base case
  if (num <= 0) {
    console.log('all done!!!');
    return;
  }

  console.log(num);
  num--;
  CountDown(num);
}

Clone this wiki locally