← JavaScript Intermediate
10 / 25
Lesson 10 — Default & named params
Friendly function inputs
A parameter is the name a function gives to an input. A default value (msg = "Hi") is used when the caller leaves that argument out. For functions with many options, take one object and destructure it — callers then name what they pass and can skip the rest.
function box({ width = 10, color = "red" } = {}) {
return width + " " + color;
}
console.log(box({ color: "blue" })); // 10 blue
Why it matters: defaults stop "undefined" from leaking into your output and make functions easy to call.
Want it another way?
Task
Give msg a default of "Hi" so greet("Sam") prints Hi Sam, while greet("Ann", "Hello") still prints Hello Ann.
Run your code — when the output matches the task, you will see a green check. Then mark the lesson complete.
Test your knowledge
A quick 3-question quiz on this lesson — instant feedback.