JS Variable Length Argument Function In Hindi

📔 : Java Script 🔗

पिछले topic में आपने JavaScript में Parameterized Functions के बारे में पढ़ा , इन functions को जब हम define करते थे तो हमें need के according parameters भी define करते थे , और उस function को Call करते समय हमें उसी के accordingly values / arguments भी pass किये जाते थे।


अब अगर हमें ये नहीं मालूम हो कि function call करते समय कितने arguments pass हो रहे हैं , इस तरह के functions को handle करने के लिए हमें JavaScript ने facility provide की है Variable Length Arguments की . जिसकी help से हम pass किये गए सभी arguments / values को easily handle कर सकते हैं।


variable number of arguments / length of arguments को handle करने के दो तरीके है -

  1. Using arguments Object .
  2. By prepending triple dots(...) in parameter.

JS arguments Object

JavaScript में arguments Array like predefined object है , जो कि pass किये गए सभी arguments को contain करता है। array like means arguments को Array की तरह  access कर सकते हैं , but Array function like map() या forEach() नहीं use कर सकते हैं।

JS Variable Length Argument Function Example Using arguments Object

File : js_var_len_fun.html

Copy Fullscreen Close Fullscreen Run
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>JavaScript Variable Length Arguments Function Example Using arguments Object</title>
  </head>
  <body>
    <script type="text/javascript">
      function test_fun()
      {
        document.write(`Total passed arguments : ${arguments.length} <br>`);
        for(index in arguments) {
          document.write(`Index ${index} Value : ${arguments[index]} <br>`);
        }

        /* accessing individual  */
        document.write(`First argument: ${arguments[0]} <br>`);
      }
      /*call function by passig some arrgument*/
      test_fun(46767,67,678,8,8,6532);
    </script>
  </body>
</html>
Output
Total passed arguments : 6
Index 0 Value : 46767
Index 1 Value : 67
Index 2 Value : 678
Index 3 Value : 8
Index 4 Value : 8
Index 5 Value : 6532
First argument: 46767

ऊपर example आप देख सकते हैं , कि किस तरह से arguments Object का use करके function में pass किये गए arguments को handle कर सकते हैं।

? Backticks (``) का use करके define की गयी string में हम ${ } ( dollar curly braces) के अंदर हम direct variables / expression को print करा सकते हैं। और '<br>' use line break करने के लिए किया जाता है।

JS Handling Variable Length Argument Function Using triple dots(...)

function define करते समय किसी parameter से पहले triple dots (...) prepend करके भी हम सभी variables को handle कर सकते हैं जो कि हमें arguments object की तरह ही arguments का object ही return करता है।

See Example

File : js_var_len_fun2.html

Copy Fullscreen Close Fullscreen Run
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>JavaScript Variable Length Arguments Function Example Using  triple dots</title>
  </head>
  <body>
    <script type="text/javascript">
      function test_fun(...args)
      {
        document.write(`Total passed arguments : ${args.length} <br>`);
        for(index in args) {
          document.write(`Index ${index} Value : ${args[index]} <br>`);
        }
      }
      test_fun(46767,67,678,8,8,6532);
    </script>
  </body>
</html>
Output
Total passed arguments : 6
Index 0 Value : 46767
Index 1 Value : 67
Index 2 Value : 678
Index 3 Value : 8
Index 4 Value : 8
Index 5 Value : 6532

Related Topics :

Rahul Kumar

Rahul Kumar

Hi ! My name is Rahul Kumar Rajput. I'm a back end web developer and founder of learnhindituts.com. I live in Uttar Pradesh (UP), India and I love to talk about programming as well as writing technical tutorials and tips that can help to others.

Get connected with me. :) LinkedIn Twitter Instagram Facebook

b2eprogrammers