Table of Contents
Back Button
Back to Chapter
Control Statements
No items found.
Function & Events
No items found.
Array and its Types
No items found.
OOP : Object Oriented Programming
No items found.
Javascript Standards
No items found.
HTML DOM
No items found.
Javascript : Advanced 1
No items found.
Javascript : Advanced 2
No items found.
Additional JavaScript
No items found.

Recursive Function

The Function which Calles Itself is known as Recursive Function.

Example:

If I ask you to print numbers from 1 to 10 without using loop.

Can you do??? No... So, Let me introduce you to the concept of Recursion.

Syntax

// code recursion(); //code } recursion();
https://s3-us-west-2.amazonaws.com/secure.notion-static.com/a663004a-c5f5-4ccc-856e-895ea9266467/190687940_200044695275767_3315539054123519522_n.jpg

Here,

Once you call the Function recursion then the statements inside the function will run. i.e. recusion(); will be called again.

Example

var no=1; function printNo() { console.log(no); no+=1; if(no <= 10) { printNo(no); } } printNo(); // Output // 1 // 2 // 3 // 4 // 5 // 6 // 7 // 8 // 9 // 10


Here,

We have declared a Global Scope Variable no

Then Function called printNo();

Let's Understand the Function printNo();

At First, no is printed in Console.

no+=1 i.e. no = no + 1

Then we have used, If Condition

if no is less than 10 then function will be called again.

otherwise, function will be terminated.

Note:

You Should be Very Careful while using the Recursive Function. Because Many Beginners When Try to do Looping using Recursive Function, They usually Forget to Put Condition to Stop the Loop, resulting in Infinite Loop and Crashing of the Program.

So, Don't Forget to Put Condition to Stop the Loop, you can use if condition to stop the Iteration of The Loop.

Ask queries
Contact Us on Whatsapp
Hi, How Can We Help You?