JavaScript Arrow Function In Hindi

📔 : Java Script 🔗

पिछले topic में आपने Anonymous function के बारे में पढ़ा , जो normal function से काफी short था।

but JavaScript में function define करने का बहुत ही easy और short method है , Arrow Function .


जब हमें short expression calculate / evaluate करना हो तो complete function define करने के जगह Arrow Function use कर सकते हैं।

JS Arrow Function Syntax

  let test = () => expression;

Arrow Function में curly braces और return keyword की जरूरत नहीं पड़ती है।

Arrow Function में direct parenthesis के बाद expression लिखते हैं , और expression evaluate होकर automatically return हो जाता है।

JS Arrow Function Example

File : js_arrow_fun.html

Copy Fullscreen Close Fullscreen Run
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>JavaScript Arrow Function In Hindi</title>
  </head>
  <body>
     <script type="text/javascript">
      let test = () => "Hello Arrow !";
      document.write(test());
    </script>
  </body>
</html>
Output
Hello Arrow !

? Arrow Function , Anonymous Function का ही Shorter version है।

JS Multiline Arrow Function

Important
Generally Arrow Function तभी use करते हैं जब हम one line expression evaluate करना हो , But अगर कही भी multi-line expressions / statements हैं तो arrow (=>) के बाद curly braces के अंदर सभी expressions / statements return statement के साथ लिखते हैं।

इसके अलावा Normal Functions की तरह ही parameters / default parameters भी define कर सकते हैं।

See Example

File : js_arrow_fun2.html

Copy Fullscreen Close Fullscreen Run
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>JavaScript Multiline Arrow Function In Hindi</title>
  </head>
  <body>
    <script type="text/javascript">
      let test = (name , age, address) => {
        return `Name : ${name} <br> Age : ${age} <br> Adderss : ${address}`;
      };
      document.write(test("Rahul Rajput", 27, "Jaipur"));
    </script>
  </body>
</html>
Output
Name : Rahul Rajput
Age : 27
Adderss : Jaipur

JS Arrow Function : Important Note

  • अगर कोई single variable ही pass करना है , तो parenthesis भी remove कर सकते हैं।
    let test = x => x+90;
    test(10); /* O/P : 100 */
    
  • Arrow Function , normal function की तरह as a constructor use नहीं हो सकते हैं।
    let test = x => x+90;
    let con = new test(); /* will throw en error TypeError: test is not a constructor*/
    
  • और न ही normal function की तरह इनमे prototype property होती है।
    let test = x => x+90;
    console.log(test.prototype); /* undefined */
    

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