DEV Community

Cover image for Understand Recursion: Most Basic Example
baijanathTharu
baijanathTharu

Posted on • Edited on

Understand Recursion: Most Basic Example

Take a look

function countDown(n) {
  if (n <= 0) return;
  console.log(n);
  n--;
  countDown(n);
}

countDown(100);

  • A recursion basically means calling the same thing again and again until certain condition is met.
  • The above program counts down from 100 to 1 recursively.
  • As in above example, we called the countDown function inside itself until the argument n becomes 0 or less than 0.
  • The condition that is to be met is called base case. It is the condition that terminates the program.
  • If the base case is omitted, we are stuck in a loop calling the function again and again.

Top comments (1)

Collapse
 
flemer profile image
flemer

Thank you