JavaScript Function In Hindi

📔 : Java Script 🔗

Function reusable piece of code या block of code होता है जो कि कोई specific task perform करता है।  एक बार  define करने के बाद हम इन्हें  script में कितनी ही बार use / call  कर सकते हैं।  इसके अलावा JavaScript में 1000+ predefined useful function हैं जिन्हें  हम Built In Functions कहते हैं।
JavaScript में function एक object है।


हालाँकि JavaScript हमें ये facility provide करती है कि user खुद के function define कर सके जिन्हें User Defines Functions कहते हैं । Web page load होते समय कोई भी function automatically run नहीं होता है जब तक कि हम उसे किसी event handler की help से या manually call / invoke न करें।


Important
JavaScript में Function Objects होते हैं , new keyword के through आप function Object (Arrow Function को छोड़कर) बना सकते हैं। इसलिए functions को आप किसी variable में assign भी कर सकते है , और किसी function में as an argument pass भी कर सकते हैं। ( जिन्हे Callback Function कहते हैं )

JS Function Declaration

function declaration को function definition या function statement भी कहते हैं , JavaScript में function define करने के लिए function keyword का use किया जाता है।

JS Function Syntax

function function_name()
{
  //perform task here
  return something /*it's optional*/
}

Explanation

  • function_name , कोई भी valid name हो सकता है , जो कि string या underscore के साथ start हो और JavaScript में predefined keywords से match नहीं करता हो।
  • JavaScript में function name number से start नहीं होता है , numbers को  function name के बीच में या last में दे सकते हैं। But कहीं भी floating point numbers नहीं दे सकते हैं।
  • return statement, function run होने के बाद value return करता है , हालाँकि यह optional है।

JS Function Example

File : js_function.html

Copy Fullscreen Close Fullscreen Run
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>JavaScript Function In Hindi</title>
  </head>
  <body>
    <script type="text/javascript">
      function my_fun()
      {
        document.write(`Hello JS ! this is my first function.`);
      }
      my_fun();
    </script>
  </body>
</html>
Output
Hello JS ! this is my first function.

ऊपर दिए गए Example में एक simple without return statement के function define किया गया है , और उसके बाद उसे call / invoke किया गया है।

JS Function : Important Tips

  • JavaScript में Functions Case Sensitive होते हैं , means myfun() और Myfun() दोनों function different होंगे।
  • आगर आप same name के function define करते हो तो JavaScript हमेशा last वाला function ही call / invoke कराती है।

JS Function With Return Type

अब हम एक और function example देखेंगे जो कि कोई value return करता है।

File : js_function2.html

Copy Fullscreen Close Fullscreen Run
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>JavaScript Function In Hindi</title>
  </head>
  <body>
    <script type="text/javascript">
        function my_fun()
        {
          return `this is function with return type.`;
        }
        document.write(my_fun());
      </script>
    </script>
  </body>
</html>
Output
this is function with return type.

JS Function Advantages

  1. Code Re-usability : functions use करने का  सबसे बड़ा advantage यही है कि , हम code को reuse कर सकते हैं। same processing के लिए एक बार function define  करने के बाद उसे हम कही भी और कितनी बार भी use कर सकते हैं।
  2. Less Code : चूंकि हम same code के लिए functions  use करते हैं जिससे Program की length कम जाती है।
  3. Reduce Coding Time : Function use करने से  coding time reduce होता है , जो कि किसी भी developer के लिए important है।
  4. Easy To Understand : Code को समझना आसान हो जाता है।

JS Function Types

ऊपर जो function examples दिए गए हैं यह simple and regular functions थे , इनके अलावा JavaScript में functions कुछ इस प्रकार हैं।

  1. Parameterized Function
  2. Variable Length Argument Function
  3. Named Or Anonymous Function
  4. Arrow Function
  5. Callback Function
  6. Recursive Function

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