Parameter
A named input in the function definition.
Parameters define the inputs a function expects. Arguments are the actual values passed when the function is called.
function createLabel(name = "Untitled") {
return `Label: ${name}`;
}
console.log(createLabel("Settings"));
Functions & Scope
Parameters are the names listed in a function definition. Arguments are the values supplied at call time. The distinction matters when reading errors and designing APIs.
JavaScript supports default parameters, rest parameters and destructured parameters. These tools help make function inputs explicit and flexible.
Avoid functions with long, mysterious argument lists. When a function needs many options, an options object is usually easier to read.
A named input in the function definition.
A value passed into a function call.
Fallback value when an argument is missing or undefined.
Collects remaining arguments into an array.
Examples
function createButtonLabel({ text, count = 0 }) {
return `${text} (${count})`;
}
createButtonLabel({ text: "Messages", count: 3 });
createButtonLabel("Messages", 3, true, false);
// The call is compact, but the meaning of each value is hidden.
Code patterns
These examples focus on syntax you will actually look up later: declarations, callbacks, returns, closures and context handling. Use the small patterns first, then study the deeper explanation.
Provide a fallback for missing input.
function createLabel(value = "Untitled") {
return `Label: ${value}`;
}
console.log(createLabel());
Collect any number of values into an array.
function sum(...numbers) {
return numbers.reduce((total, value) => total + value, 0);
}
console.log(sum(1, 2, 3));
Named options make calls easier to understand.
function formatStatus({ status, uppercase = false }) {
const label = `Status: ${status}`;
return uppercase ? label.toUpperCase() : label;
}
console.log(formatStatus({ status: "ready", uppercase: true }));
Extract named properties directly in the parameter list.
function getUserLabel({ name, role }) {
return `${name} (${role})`;
}
console.log(getUserLabel({ name: "User A", role: "admin" }));
Rules that matter
Functions are where JavaScript stops being a list of statements and becomes a system. Scope decides what each function can see, while return values and parameters decide how functions communicate.
Many positional parameters are hard to call correctly.
Defaults document fallback behavior.
Rest parameters are clearer than the old arguments object.
Named properties make calls self-documenting.
Parameters describe shape, but runtime values can still be wrong.
A function should not surprise callers by changing their data.
Production thinking
Function inputs are the contract between caller and function. A good contract prevents wrong calls.
UI helper functions often accept labels, roles and state. Clear parameters help prevent missing or unclear accessible text.
Production functions should use explicit parameters, defaults and validation around external input.
Functions that generate content should make required text values obvious through clear parameters.
Live code lab
The preview runs inside an isolated iframe. The JavaScript is placed inside the HTML editor for now, so every example stays together and remains easy to understand.
Mini assignment
Practice assignment
Try it yourself
Self-check
Functions only become useful when you can explain what goes in, what comes out and which scope the function can see.