📘 Lesson  ·  Lesson 21

Parameters and Arguments

Parameters और Arguments

Parameters and Arguments — passing data in

function greet(name) {          // "name" is a PARAMETER (a placeholder)
    console.log("Hello, " + name + "!");
}

greet("Aman");                  // "Aman" is an ARGUMENT (the real value)
greet("Priya");                 // reuse with a different value
// Output: Hello, Aman!  /  Hello, Priya!
Parameters let a function accept data from outside, making it flexible instead of fixed. Without them, greet() could only ever say one name. The terminology: a parameter is the placeholder name you write in the function's parentheses when DEFINING it; an argument is the actual value you pass in when CALLING it. Think of the parameter as an empty labelled box the function keeps, and the argument as what you drop into it. Inside the function, the parameter behaves exactly like a normal variable.

Multiple Parameters — order matters

function introduce(name, age, city) {
    console.log(`${name} is ${age} years old, from ${city}.`);
}

introduce("Aman", 20, "Aligarh");
// Aman is 20 years old, from Aligarh.

introduce(20, "Aman", "Aligarh");     // ✗ WRONG ORDER — nonsense output
// 20 is Aman years old, from Aligarh.
Separate multiple parameters with commas — and understand that arguments are matched to parameters by POSITION, not by name. The first argument fills the first parameter, the second fills the second, and so on. Swap the order when calling and JavaScript won't warn you; it'll happily produce nonsense. This is a real source of bugs when functions take several same-typed arguments. If a function needs more than about three parameters, it's often clearer to pass a single object instead (something you'll appreciate after the objects chapter).

Missing Arguments — undefined, not an error

function greet(name) {
    console.log("Hello, " + name);
}

greet();          // "Hello, undefined"   ← no error, just undefined!
greet("A", "B");  // extra arguments are simply ignored
JavaScript is unusually forgiving about arguments — and that's a double-edged sword. If you call a function without passing an argument, the parameter is simply undefined; JavaScript does NOT throw an error. Similarly, passing extra arguments causes no complaint — they're just ignored. This flexibility means missing-argument bugs fail silently, producing "undefined" in your output rather than a clear error. That's exactly the problem default parameters solve.

Default Parameters — sensible fallbacks

function greet(name = "Guest") {      // default value if none passed
    console.log("Hello, " + name);
}

greet("Aman");      // Hello, Aman
greet();            // Hello, Guest    ← uses the default

// Defaults work for any parameter:
function calcTotal(price, tax = 0.18) {
    return price + (price * tax);
}
calcTotal(100);        // 118  (uses default tax)
calcTotal(100, 0.05);  // 105  (overrides it)
A default parameter provides a fallback value used when that argument isn't supplied. Write = value after the parameter name. This turns the silent undefined problem into sensible behaviour: greet() now says "Hello, Guest" instead of "Hello, undefined". Defaults are especially useful for optional settings — a tax rate, a currency symbol, a message. Note the default only kicks in when the argument is undefined (i.e. genuinely missing); passing 0 or "" still overrides it, since those are real values.

Rest Parameters — accepting any number of arguments

function sumAll(...numbers) {         // "..." collects ALL arguments into an array
    let total = 0;
    for (let n of numbers) {
        total += n;
    }
    return total;
}

sumAll(1, 2, 3);           // 6
sumAll(1, 2, 3, 4, 5);     // 15   — any number of arguments!
Sometimes you don't know how many arguments a function will receive. Rest parameters (...name) gather all remaining arguments into an ARRAY. Inside sumAll, numbers is a real array you can loop over. This is how functions like "add up everything I give you" are built. The three dots must come last in the parameter list, and there can only be one rest parameter. You'll see those three dots again as the "spread" operator, which does the reverse — a topic in the Modern JS group.

Exam Corner

Q: Difference between a parameter and an argument? A parameter is the placeholder in the definition; an argument is the real value passed when calling.

Q: What happens if you don't pass an argument? The parameter is undefined — no error is thrown.

Q: How do you give a parameter a default value? function greet(name = "Guest") {}

Q: How are arguments matched to parameters? By position (order), not by name.

Q: What do rest parameters (...args) do? Collect any number of remaining arguments into an array.

Parameters और Arguments — data भेजना

function greet(name) {          // "name" PARAMETER hai (placeholder)
    console.log("Hello, " + name + "!");
}

greet("Aman");                  // "Aman" ARGUMENT hai (asli value)
greet("Priya");                 // alag value se reuse
// Output: Hello, Aman!  /  Hello, Priya!
Parameters function को बाहर से data स्वीकार करने देते हैं, उसे fixed के बजाय flexible बनाते. उनके बिना, greet() सिर्फ एक नाम कह सकता था. शब्दावली: parameter वह placeholder नाम है जो आप function के parentheses में DEFINE करते समय लिखते हैं; argument वह असली value है जो आप CALL करते समय भेजते हैं. Parameter को खाली labelled box समझिए जो function रखता है, और argument वह जो आप उसमें डालते हैं. Function के अंदर, parameter बिल्कुल सामान्य variable जैसा व्यवहार करता है.

कई Parameters — order मायने रखता है

function introduce(name, age, city) {
    console.log(`${name} is ${age} years old, from ${city}.`);
}

introduce("Aman", 20, "Aligarh");
// Aman is 20 years old, from Aligarh.

introduce(20, "Aman", "Aligarh");     // ✗ GALAT ORDER — bematlab output
// 20 is Aman years old, from Aligarh.
कई parameters commas से अलग कीजिए — और समझिए arguments parameters से POSITION से match होते हैं, नाम से नहीं. पहला argument पहला parameter भरता है, दूसरा दूसरा, वगैरह. Call करते समय order बदलिए और JavaScript आपको चेतावनी नहीं देगा; यह खुशी-खुशी बेमतलब नतीजा देगा. जब functions कई एक-जैसे-type के arguments लें तो यह असली bugs का स्रोत है. अगर function को तीन से ज़्यादा parameters चाहिए, अक्सर एक object भेजना ज़्यादा साफ है (जो objects chapter के बाद आप सराहेंगे).

गायब Arguments — undefined, error नहीं

function greet(name) {
    console.log("Hello, " + name);
}

greet();          // "Hello, undefined"   ← koi error nahi, bas undefined!
greet("A", "B");  // extra arguments bas ignore ho jate hain
JavaScript arguments के बारे में असामान्य रूप से उदार है — और यह दोधारी तलवार है. अगर आप बिना argument भेजे function call करें, parameter बस undefined होता है; JavaScript error NAHI फेंकता. वैसे ही, extra arguments भेजने पर कोई शिकायत नहीं — वे बस ignore होते हैं. यह लचीलापन मतलब missing-argument bugs चुपचाप fail होते हैं, साफ error के बजाय आपके output में "undefined" देते. यही समस्या default parameters हल करते हैं.

Default Parameters — समझदार fallbacks

function greet(name = "Guest") {      // koi na bheje to default value
    console.log("Hello, " + name);
}

greet("Aman");      // Hello, Aman
greet();            // Hello, Guest    ← default use karta hai

// Defaults kisi bhi parameter par chalte hain:
function calcTotal(price, tax = 0.18) {
    return price + (price * tax);
}
calcTotal(100);        // 118  (default tax use karta)
calcTotal(100, 0.05);  // 105  (override karta)
Default parameter fallback value देता है जो तब use होता है जब वह argument न दिया जाए. Parameter नाम के बाद = value लिखिए. यह चुपचाप undefined वाली समस्या को समझदार व्यवहार में बदलता है: greet() अब "Hello, undefined" के बजाय "Hello, Guest" कहता है. Defaults खासकर optional settings के लिए उपयोगी हैं — tax rate, currency symbol, message. ध्यान default सिर्फ तब लगता है जब argument undefined हो (यानी सच में गायब); 0 या "" भेजना फिर भी उसे override करता है, क्योंकि वे असली values हैं.

Rest Parameters — कितने भी arguments स्वीकारना

function sumAll(...numbers) {         // "..." SAARE arguments ko array me ikattha karta
    let total = 0;
    for (let n of numbers) {
        total += n;
    }
    return total;
}

sumAll(1, 2, 3);           // 6
sumAll(1, 2, 3, 4, 5);     // 15   — kitne bhi arguments!
कभी आपको नहीं पता function कितने arguments पाएगा. Rest parameters (...name) सारे बचे arguments को ARRAY में इकट्ठा करते हैं. sumAll के अंदर, numbers असली array है जिस पर आप loop कर सकते हैं. ऐसे ही "जो कुछ दो सब जोड़ दो" जैसे functions बनते हैं. तीन dots parameter list में आखिरी होने चाहिए, और सिर्फ एक rest parameter हो सकता है. आप वे तीन dots फिर "spread" operator के रूप में देखेंगे, जो उल्टा करता है — Modern JS group का विषय.

Exam Corner

Q: Parameter और argument में अंतर? Parameter definition का placeholder है; argument call करते समय भेजी असली value.

Q: Argument न भेजें तो क्या होता है? Parameter undefined होता है — कोई error नहीं.

Q: Parameter को default value कैसे देते हैं? function greet(name = "Guest") {}

Q: Arguments parameters से कैसे match होते हैं? Position (order) से, नाम से नहीं.

Q: Rest parameters (...args) क्या करते हैं? कितने भी बचे arguments को array में इकट्ठा करते हैं.
← Back to JavaScript Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

इस पेज का code यहाँ तैयार है — बदलिए और तुरंत नतीजा देखिए. कुछ install किए बिना.
👁 Live Preview
सब कुछ आपके browser में ही चलता है — कोई server, कोई signup नहीं. JavaScript का console.log देखने के लिए F12 दबाइए.