Parameters, Default Values & Rest Syntax in JavaScript
🟢 Beginner Level | JavaScript Core Syntax
📖 Definition
JavaScript function parameters support Default Parameters (fallback values for missing arguments), Rest Parameters (...args for capturing variable argument lists into arrays), and Destructuring Parameters.
🌍 Multilingual Explanations
English
Default parameters handle missing arguments gracefully. Rest parameters (...args) collect remaining arguments into a real array, replacing the legacy arguments object.
Hindi
Agar koi argument pass na ho, toh default parameter fallback value le leta hai. ...args baki saare arguments ko array mein store karta hai.
💡 Production Code Example
// 1. Default Parameters & Rest Parameters
function createCartItem(title, price, quantity = 1, ...tags) {
console.log(`Item: ${title}, Qty: ${quantity}, Tags:`, tags);
return price * quantity;
}
createCartItem("Keyboard", 49.99, 2, "tech", "accessories");
// 2. Destructuring Parameter with Defaults
function configureServer({ host = "localhost", port = 8080, ssl = false } = {}) {
console.log(`Server starting on ${host}:${port} (SSL: ${ssl})`);
}
configureServer({ port: 3000, ssl: true }); // Server starting on localhost:3000 (SSL: true)
⚠️ Common Pitfalls
Rest Parameter Position: The rest parameter (...args) MUST be the last parameter in the function declaration signature!
🎯 Tiered Practice Exercises
- Beginner: Create a function
calculateDiscount(price, discountPercent = 10). - Intermediate: Write a function
sumAll(...numbers)that sums an arbitrary count of numbers usingArray.reduce(). - Advanced: Write a function taking a destructured user profile object with nested defaults.
🧭 Navigation
← 02. Arrow Functions & Lexical 'this' | Functions Hub | 04. First-Class & Higher-Order Functions →