Functions in JavaScript in Hindi: Easy Guide for Beginners
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:??