JavaScript Recursive Function In Hindi

📔 : Java Script 🔗

PHP , C and C++ की तरह ही JavaScript में भी हम recursive functions define कर सकते है।

Same Function को उसी Function के अंदर call करने को ही Recursive Function कहते हैं , और इस process को Recursion कहते हैं।

JS Need Of Recursive Function

  1. Recursive Functions का use tree structure (read या tree structure बनाने में ) में सबसे ज्यादा होता है , जहां पर हमें ये नहीं मालूम होता की Node का कोई children है या नहीं।

  2. Data Sorting के लिए भी Recursive function use कर सकते हैं।

  3. दूसरा File Directories को read करने के लिए , क्योंकि हमें नहीं मालूम होता है कि एक directory के अंदर सभी files ही है फिर उसके अंदर sub-directories और फिर उसके अंदर etc

JS Recursive Function Example

File : js_recursive_fun.html

Copy Fullscreen Close Fullscreen Run
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>JavaScript Recursive Function Example</title>
  </head>
  <body>
    <script type="text/javascript">
      function print_number(number)
      {
        /*print number*/
        document.write(number +' , ');
      
        /*increment by 1*/
        number++;
      
        /*if number is less than 100 then call the function*/
        if(number <= 100)
        {
          /*call the function*/
          print_number(number);
        }
      }
      
       print_number(1)
    </script>
  </body>
</html>
Output
1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 , 13 , 14 , 15 , 16 , 17 , 18 , 19 , 20 , 21 , 22 , 
23 , 24 , 25 , 26 , 27 , 28 , 29 , 30 , 31 , 32 , 33 , 34 , 35 , 36 , 37 , 38 , 39 , 40 , 41 , 42 , 43 , 44 , 45 , 46 , 47 , 48 , 49 , 
50 , 51 , 52 , 53 , 54 , 55 , 56 , 57 , 58 , 59 , 60 , 61 , 62 , 63 , 64 , 65 , 66 , 67 , 68 , 69 , 70 , 71 , 72 , 73 , 74 , 75 , 76 , 
77 , 78 , 79 , 80 , 81 , 82 , 83 , 84 , 85 , 86 , 87 , 88 , 89 , 90 , 91 , 92 , 93 , 94 , 95 , 96 , 97 , 98 , 99 , 100 ,

ऊपर एक simple recursive function का example दिया गया है , example में 100 बार same function call हुआ है। example को आप ध्यान से देखेंगे तो पायंगे कि एक condition दी हुई है कि agar variable की value 100 से कम या बराबर है तभी function को call किया गया है।


Note
देखा जाए तो Recursive Functions का work flow same as For Loop की तरह ही होता है , क्योंकि For Loop में भी execution एक specified condition तक repeat होता है। Condition false होने पर Loop end हो जाता था।


JS Factorial Example using Recursive Function

File : js_recursive_fun.html

Copy Fullscreen Close Fullscreen Run
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>JavaScript Recursive Function Example</title>
  </head>
  <body>
    <script type="text/javascript">
      function fact(number){
        if(number <= 1)
          return 1;
        else
          return number*fact(number-1);
      }
      document.write(`Factorial of entered number is : ${fact(5)}`);
    </script>
  </body>
</html>
Output
Factorial of 5 is : 120

Example में simply किसी number का factorial जानने के लिए code implement किया गया है , Example में एक condition दी है कि variable की value 1 से कम होने पर सिर्फ 1 ही return करता है , otherwise same function call होता रहेगा।

Recursive Function में Loop की तरह ही condition देना mandatory है , otherwise function infinite call करता रहेगा और page breach हो सकता है।

JS Disadvantages Of Recursive Function

Recursive Functions , iterative program (like  For Loop / While Loop ) के तुलना में काफी memory और time consume करते हैं। इसलिए ज्यादा जरूरी होने पर ही Recursive Functions का use करें।

Related Topics :

Rahul Kumar

Rahul Kumar

Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)

Get connected with me. :) LinkedIn Twitter Instagram Facebook

b2eprogrammers