JavaScript Anonymous Function In Hindi

📔 : Java Script 🔗

Anonymous Functions , जैसा कि नाम से ही ही मालूम होता है ऐसे functions जिनका कोई name नहीं है , JavaScript हमें ये facility provide करती है कि हम without name के functions भी define कर सके।


जैसे variable declare करते समय value को assign किया जाता है , ठीक वैसे ही function को भी assign कर सकते हैं।

JS Anonymous Function Syntax

let test = function(param1, param2)
{
  //write your logic here
}

Parameterized Functions की तरह ही इसमें हम parameters भी define कर सकते हैं।

JS Anonymous Function Example

File : js_anonymous_fun.html

Copy Fullscreen Close Fullscreen Run
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>JavaScript Anonymous Function In Hindi</title>
  </head>
  <body>
    <script type="text/javascript">
      let test = function()
      {
        document.write(`First Name : ${arguments[0]} <br> Last Name : ${arguments[1]}`);
      }
      /*we call the variable like a function */
      test('Rahul', 'Rajput');
    </script>
  </body>
</html>
Output
First Name : Rahul
Last Name : Rajput

Anonymous function में भी normal function की तरह ही variable ( जो कि function के बाहर declared है ) को access कर सकते हैं , और variable number of arguments / length of arguments को भी आसानी से handle कर सकते हैं।

See Example

File : js_anonymous_fun2.html

Copy Fullscreen Close Fullscreen Run
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>JavaScript Anonymous Function In Hindi</title>
  </head>
  <body>
    <script type="text/javascript">
      let ex_var = 'A Variable';
      let test = function()
      {
        document.write(`Full Name : ${arguments[0]} <br>`);
        document.write(`External Variable : ${ex_var}`);
      }
      test('Rahul Rajput');
    </script>
  </body>
</html>
Output
Full Name : Rahul Rajput
External Variable : A Variable

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