Combining Default and Rest Parameters
You'll learn how to use default and rest parameters together to create flexible functions that handle both optional values and variable argument lists.
The Core Idea
You can mix default parameters with rest parameters in the same function signature to handle multiple scenarios: some arguments get sensible fallback values, while others collect any remaining arguments into an array. This combination gives you maximum flexibility when designing function APIs.
The key rule: default parameters come before rest parameters. This makes sense because rest parameters must always be last—they're the "catch-all" for everything else.
How It Works
function createProfile(name = "Anonymous", age = 0, ...hobbies) {
console.log(`Name: ${name}, Age: ${age}`);
console.log(`Hobbies: ${hobbies.join(", ")}`);
}
createProfile();
// Name: Anonymous, Age: 0
// Hobbies:
createProfile("Alice", 25, "reading", "hiking", "coding");
// Name: Alice, Age: 25
// Hobbies: reading, hiking, coding
createProfile("Bob", undefined, "gaming");
// Name: Bob, Age: 0
// Hobbies: gaming
Here, name and age get defaults if omitted or undefined, while hobbies collects all remaining arguments into an array—even if there are zero of them (it becomes an empty array).
Why It Matters
This pattern is perfect for functions like logging utilities, data processors, or API wrappers where you want required-but-defaulted configuration alongside an open-ended list of items to process.
Key Takeaway: Default parameters provide fallback values for the first few arguments, while rest parameters gather all remaining arguments into an array—combine them to create functions that are both predictable and flexible.