If tutorials available on this website are helpful for you, please whitelist this website in your ad blocker😭 or Donate to help us ❤️ pay for the web hosting to keep the website running.
पिछले 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 कर सकते हैं।
let test = () => expression;
Arrow Function में curly braces और return keyword की जरूरत नहीं पड़ती है।
Arrow Function में direct parenthesis के बाद expression लिखते हैं , और expression evaluate होकर automatically return हो जाता है।
File : js_arrow_fun.html
<!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>
? Arrow Function , Anonymous Function का ही Shorter version है।
❕ 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
<!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>
let test = x => x+90; test(10); /* O/P : 100 */
let test = x => x+90; let con = new test(); /* will throw en error TypeError: test is not a constructor*/
let test = x => x+90;
console.log(test.prototype); /*
*/
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