What arrow functions are
Arrow Function JavaScript में function लिखने का एक short और modern तरीका है।
इससे code छोटा और ज्यादा readable हो जाता है।
Normal Function :
function greet(name) {
return "Hello " + name;
}
Arrow Function :
const greet = (name) => {
return "Hello " + name;
};
Basic arrow function syntax
Arrow function का basic syntax:
const functionName = (parameters) => {
// code
};
यहाँ => को arrow syntax कहते हैं।
const add = (a, b) => {
return a + b;
};
Arrow functions with one parameter
जब arrow function में सिर्फ एक parameter होता है, तो parentheses () लगाना optional होता है।
const square = x => x * x;
console.log(square(5)); // 25
Arrow functions with multiple parameters
जब एक से ज्यादा parameters होते हैं, तब parentheses जरूरी होते हैं।
const add = (a, b) => a + b;
console.log(add(3, 4)); // 7
Implicit return vs explicit return
Implicit Return
जब function एक ही expression return करता है, तो return लिखने की जरूरत नहीं होती।
const multiply = (a, b) => a * b;
console.log(multiply(2, 3)); // 6
Explicit Return
जब function में multiple lines होती हैं, तो {} और return लिखना जरूरी होता है।
const multiply = (a, b) => {
return a * b;
};
console.log(multiply(2, 3));

