JavaScript में Functions reusable और modular कोड ब्लॉक्स हैं, जो abstraction और code organization को सपोर्ट करते हैं। यह ब्लॉग Function Declaration, Expression, Arrow Functions, Parameters, Default Parameters, Rest Parameters, Return Statements, Function Scope, और Anonymous Functions को सरल भाषा में समझाता है, जिसमें टेक्निकल टर्म्स और थ्योरी शामिल हैं।
साथ ही, प्रैक्टिकल एक्सरसाइज (फैक्टोरियल और स्ट्रिंग रिवर्सल), 5 MCQ क्विज़, और मिनी प्रोजेक्ट (टिप कैलकुलेटर) आपके JavaScript स्किल्स को बढ़ाएंगे। यह गाइड हिंदी में है, टेक्निकल टर्म्स (जैसे function, scope, closure) English में हैं, और कोड पूरी तरह English में है।
Types of Functions in JavaScript with Examples in Hindi (JavaScript में फंक्शन्स के प्रकार उदाहरणों सहित)
JavaScript में functions कई प्रकार के होते हैं – जैसे declaration, expression, arrow functions आदि। ये सभी प्रकार कोड को modular और reusable बनाने में मदद करते हैं।
Functions encapsulated कोड ब्लॉक्स हैं, जो reusability, modularity, और abstraction को इनेबल करते हैं। Execution context और call stack के साथ runtime में invoke होते हैं। गलत parameters या scope से reference errors हो सकते हैं।
- Best Practice: single responsibility फॉलो करें, let/const यूज़ करें।
- Use Case: डेटा प्रोसेसिंग, event handling।
- Pitfalls: undefined returns, global scope pollution।
Example:
function sayHello() {
console.log("Hello, World!");
}
sayHello(); // Hello, World!
Function Declaration:
Function Declaration function कीवर्ड से फंक्शन को declare करता है। Hoisting की वजह से call से पहले invoke संभव। Scope को block scope (let/const) से सिमिट करें। गलत parameter handling से logic errors।
Example:
function add(a, b) {
return a + b;
}
console.log(add(2, 3)); // 5
Function Expression:
Function Expression फंक्शन को variable में assign करता है। Hoisting नहीं होता, declaration पहले जरूरी। Scope variable scope पर डिपेंड करता है। Closure सपोर्ट करता है।
Example:
const multiply = function(a, b) {
return a * b;
};
console.log(multiply(2, 3)); // 6
Arrow Functions:
Arrow Functions (=>) concise syntax और lexical this बाइंडिंग देते हैं। Closure और callback के लिए आइडियल। नेस्टेड arrow functions से readability इश्यू।
Example:
const subtract = (a, b) => a - b;
console.log(subtract(5, 3)); // 2
Parameters:
Parameters फंक्शन को dynamic inputs देते हैं। Arguments runtime में pass होते हैं। गलत parameter types से type errors। बेस्ट प्रैक्टिस: type checking करें।
Example:
function greet(name) {
console.log(`Hello, ${name}!`);
}
greet("Alice"); // Hello, Alice!
Default Parameters:
Default Parameters के लिए fallback values सेट करते हैं। Undefined inputs से बचाते हैं। Scope block scope में सिमिट। गलत डिफॉल्ट से logic errors।
Example:
function welcome(name = "Guest") {
console.log(`Welcome, ${name}!`);
}
welcome(); // Welcome, Guest!
Rest Parameters:
Rest Parameters (…args) variable arguments को array में कलेक्ट करते हैं। Dynamic input handling के लिए। गलत यूज़ से array methods में इश्यू। बेस्ट प्रैक्टिस: reduce/map यूज़ करें।
Example:
function sum(...numbers) {
return numbers.reduce((total, num) => total + num, 0);
}
console.log(sum(1, 2, 3, 4)); // 10
Return Statements:
Return Statements फंक्शन से output रिटर्न करते हैं। Execution को तुरंत terminates करते हैं। बिना return के undefined रिटर्न। बेस्ट प्रैक्टिस: सिंगल return value।
Example:
function square(num) {
return num * num;
}
console.log(square(4)); // 16
Function Scope:
Function Scope वैरिएबल्स को local scope में सिमिट करता है। Lexical scope और closure outer variables को एक्सेस करने देते हैं। गलत scope से reference errors। बेस्ट प्रैक्टिस: let/const यूज़ करें।
Example:
function testScope() {
let x = 10;
console.log(x); // 10
}
testScope();
console.log(typeof x); // undefined
Anonymous Functions:
Anonymous Functions बिना identifier के फंक्शन्स हैं, callbacks या IIFE (Immediately Invoked Function Expression) में यूज़ होते हैं। Scope सिमिट। गलत यूज़ से debugging इश्यू।
Example:
setTimeout(function() {
console.log("Delayed Hello!");
}, 1000);
Exercise: Factorial and String Reversal Functions
Task 1: Factorial कैलकुलेट करने वाला फंक्शन।
Task 2: स्ट्रिंग को reverse करने वाला फंक्शन।
return, parameters, और validation यूज़ करें। गलत इनपुट से type errors। बेस्ट प्रैक्टिस: input validation। यूज़ केस: mathematical computations, text processing।
Code:
function factorial(n) {
if (typeof n !== "number" || n < 0 || !Number.isInteger(n)) return "Invalid input";
if (n === 0) return 1;
let result = 1;
for (let i = 1; i <= n; i++) {
result *= i;
}
return result;
}
console.log(factorial(5)); // 120
function reverseString(str) {
if (typeof str !== "string") return "Invalid input";
return str.split("").reverse().join("");
}
console.log(reverseString("hello")); // olleh
Quiz: Function Syntax & Scope
What is the output of add(2, 3) in the function add(a, b) { return a + b; }?
A) 6
B) 5
C) Undefined
D) Error
Answer: B) 5
What is the output of multiply(4, 5) in const multiply = (a, b) => a * b;?
A) 20
B) 9
C) Undefined
D) Error
Answer: A) 20
What is the output of greet() in the function greet(name = \”User\”) { return name; }?
A) User
B) Undefined
C) null
D) Error
Answer: A) User
What is the output of console.log(typeof x) in the function test() { let x = 10; }?
A) number
B) undefined
C) Error
D) object
Answer: B) undefined
What is the behavior of setTimeout(function() { console.log(\”Hi\”); }, 1000);?
A) तुरंत “Hi” प्रिंट
B) 1 सेकंड बाद “Hi” प्रिंट
C) Error
D) कुछ नहीं
Answer: B) 1 सेकंड बाद “Hi” प्रिंट
Mini Project: Tip Calculator Function
Task: बिल अमाउंट और टिप परसेंटेज लेकर tip amount और total amount कैलकुलेट करें। default parameters, return, और validation यूज़ करें। गलत इनपुट से type errors। बेस्ट प्रैक्टिस: input validation। यूज़ केस: billing applications।
Code:
function calculateTip(bill, tipPercent = 10) {
if (typeof bill !== "number" || bill <= 0 || typeof tipPercent !== "number" || tipPercent < 0) {
return "Invalid input";
}
const tipAmount = bill * (tipPercent / 100);
const total = bill + tipAmount;
return { tipAmount, total };
}
console.log(calculateTip(100)); // { tipAmount: 10, total: 110 }
console.log(calculateTip(100, 15)); // { tipAmount: 15, total: 115 }
Conclusion
Functions JavaScript में reusability, modularity, और abstraction को सपोर्ट करते हैं। Declaration, Expression, और Arrow Functions से tasks आसान हो जाते हैं। प्रैक्टिस करें और अगले मॉड्यूल में Objects सीखें।
Also Read: