JS ऐरो फंक्शन्स
In this page:
const functionName = (parameters) => expression;
const functionName = (parameters) => {
// body
return value;
};
Arrow Function Syntax
एक arrow function, function keyword की जगह parameter list के बाद => रखता है, और किसी single expression के लिए, curly braces और return keyword दोनों को पूरी तरह छोड़ा जा सकता है।
उदाहरण: Arrow Function Syntax
const add = (a, b) => a + b;
console.log(add(2, 3));
Implicit Return
जब किसी arrow function का body एक single expression हो, तो आप curly braces और return keyword दोनों छोड़ सकते हैं, JavaScript अपने आप expression का result return कर देता है।
उदाहरण: Implicit Return
const square = n => n * n; // implicit return, no braces needed
console.log(square(5));
Array Methods में Arrow Functions
map, filter, और forEach जैसे array methods को पास किए गए छोटे, inline callbacks के रूप में arrow functions बहुत आम हैं, जो code को compact और readable बनाए रखते हैं।
उदाहरण: Arrow Functions in Array Methods
const nums = [1, 2, 3];
console.log(nums.map(n => n * 2));
Arrow Functions में this
regular functions के उलट, arrow functions का अपना this नहीं होता, वे उस enclosing scope से this inherit करते हैं जहाँ arrow function define किया गया था, जो context के हिसाब से मददगार या हैरान करने वाला दोनों हो सकता है।
उदाहरण: this in Arrow Functions
const obj = {
value: 42,
regular: function () {
const arrow = () => console.log(this.value); // inherits this from regular()
arrow();
},
};
obj.regular();
Arrow Functions कब इस्तेमाल न करें
ऐसे object methods के लिए arrow functions से बचना बेहतर है जिन्हें this चाहिए, और इन्हें new के साथ constructor functions के रूप में इस्तेमाल नहीं किया जा सकता, इन खास मामलों में regular function declarations ही सही विकल्प रहते हैं।
उदाहरण: When NOT to Use Arrow Functions
const obj = {
value: 42,
// Avoid: arrow function as a method, 'this' won't refer to obj
broken: () => console.log(this),
};
obj.broken(); // this is NOT obj
- किसी object method की तरह arrow function इस्तेमाल करना जो this पर निर्भर करता है, arrow functions का अपना this नहीं होता, जिससे अनपेक्षित व्यवहार होता है।
- यह भूल जाना कि arrow function syntax में कई parameters के लिए parentheses ज़रूरी हैं, लेकिन एक single parameter के लिए optional।
- बिना parentheses के implicitly return होने वाले किसी object literal को wrap करना, जिसे JavaScript function body block के रूप में गलत समझता है।
- Arrow functions => syntax इस्तेमाल करते हैं और किसी single expression के लिए curly braces और return छोड़ सकते हैं, जिसे implicit return कहते हैं।
- Arrow functions का अपना this नहीं होता, वे उस surrounding scope से this inherit करते हैं जहाँ वे defined थे।
- Arrow functions छोटे callbacks के लिए ideal हैं, जैसे map या filter के अंदर, लेकिन object methods के लिए उतने उपयुक्त नहीं।
Arrow functions को 2015 से सभी major browsers में support मिला है और यह modern JavaScript का पूरी तरह standard हिस्सा है।
Chapter Quiz — Complete all 26 topics to unlock
0/26 topics done
Complete these topics first:
- JS Dates
- JS Math
- JS Conditionals
- JS Switch
- JS Loop For
- JS Loop While
- JS Iterables
- JS Sets
- JS Maps
- JS typeof
- JS Type Conversion
- JS Destructuring
- JS Arrow Functions
- JS Classes
- JS Modules
- JS Promises
- JS Async/Await
- JS DOM
- JS DOM Methods
- JS Events Advanced
- JS DOM Navigation
- JS DOM Collections
- JS Async Callbacks
- JS Async Parallel
- JS Date Set
- JS Set Logic