JavaScript Functions Kya Hote Hain?

Reusable code likhna aur apne application ko structure karna sikhein.

Functions ka Introduction

JavaScript me function ek reusable block of code hota hai jo ek specific task perform karta hai. Functions code ko organized, modular, aur maintainable banane me madad karte hain. Ek baar function define karne ke baad, aap use code me kahin bhi, kitni bhi baar call kar sakte hain.

Types of Functions in JavaScript

JavaScript me functions ko kai tariko se define kiya jaa sakta hai. Aaiye kuch common types ko dekhte hain.

1. Function Declaration (Named Function)

Yeh function define karne ka traditional tarika hai. Inhe 'hoist' kiya jaata hai, yaani aap inhe define karne se pehle bhi call kar sakte hain.

// Function define karna
function greet() {
    console.log("Hello, Welcome to JavaScript!");
}

// Function call karna
greet(); // Output: Hello, Welcome to JavaScript!

2. Function Expression

Isme, function ko ek variable me assign kiya jaata hai. Yeh functions hoist nahi hote, isliye inhe define karne ke baad hi call kiya ja sakta hai.

const sum = function(a, b) {
    return a + b;
};

console.log(sum(5, 3)); // Output: 8

3. Arrow Function (ES6)

Arrow functions ES6 me introduce hue the aur yeh function likhne ka ek short aur concise tarika hai. Inka `this` keyword ka behavior bhi alag hota hai.

const multiply = (a, b) => a * b;
console.log(multiply(4, 5)); // Output: 20

4. Immediately Invoked Function Expression (IIFE)

IIFE ek aisa function hai jo define hote hi turant execute ho jaata hai. Yeh private scope create karne ke liye use hota hai.

(function() {
    console.log("IIFE function executed!");
})();

Advanced Function Concepts

Higher-Order Functions

Yeh woh functions hote hain jo ya toh doosre functions ko as argument lete hain, ya ek function ko return karte hain. `map`, `filter`, aur `reduce` iske common examples hain.

function operate(a, b, operation) {
    return operation(a, b);
}
const addition = (x, y) => x + y;
console.log(operate(10, 5, addition)); // Output: 15

Callback Functions

Callback ek aisa function hota hai jo doosre function ko as argument pass kiya jaata hai aur uss function ke complete hone ke baad execute hota hai. Yeh asynchronous programming me bohot use hota hai.

function fetchData(callback) {
    console.log("Fetching data...");
    // Simulating a delay
    setTimeout(function() {
      console.log("Data fetched successfully!");
      callback();
    }, 1000);
}

fetchData(function() {
    console.log("Callback function executed!");
});

Function Parameters & Scope

Functions parameters (inputs) le sakte hain aur `return` keyword ka use karke value (output) de sakte hain.

function greet(name = "Guest") { // "Guest" is a default parameter
    console.log(`Hello, ${name}!`);
}

greet(); // Output: Hello, Guest!
greet("Sandeep"); // Output: Hello, Sandeep!

Variables jo function ke bahar declare hote hain, unka scope global hota hai. Jo andar declare hote hain, unka scope local hota hai aur woh bahar se access nahi kiye jaa sakte.

Key Takeaways

  • Functions reusable code blocks hote hain jo specific tasks perform karte hain.
  • Function Declarations ko define karne se pehle call kiya ja sakta hai (hoisting).
  • Function Expressions aur Arrow Functions modern aur concise syntax provide karte hain.
  • Higher-Order Functions aur Callbacks advanced aur functional programming patterns ke liye zaroori hain.
Bonus: Practical Application!
Ab in concepts ko practically use karke dekhein.

Ek function banayein jo do numbers ko as parameter le aur unka sum return kare. Fir us function ko call karke result `console.log()` me check karein.

Practice in JS Editor
Test Your Knowledge!
Kya aap JavaScript Functions ke baare mein seekh chuke hain? Chaliye dekhte hain!

Apni knowledge test karne ke liye is quick quiz ko dein.

Start Quiz