Sub topic 1
Parameters and Arguments
A function's signature tells callers what it expects. Before looking at defaults, rest parameters, or return values, you need a clear picture of the two names for the values that flow into a function. This chapter separates them and shows how JavaScript matches one to the other.
Prerequisites
This chapter assumes you are comfortable with:
- Basic JavaScript syntax, including
letandconst - Declaring and calling functions
- Working with arrays and objects
- Template literals
The Two Terms
A parameter is a name listed in a function's definition. An argument is the actual value supplied when the function is called. Parameters are placeholders written once. Arguments are concrete values that can change with every call.
function formatPrice(amount, currency) { // amount, currency: parameters
return `${amount.toFixed(2)} ${currency}`;
}
console.log(formatPrice(9.5, "EUR")); // 9.5 and "EUR" are arguments: "9.50 EUR"
console.log(formatPrice(120, "USD")); // new arguments, same parameters: "120.00 USD"People often mix up the words in conversation, and that rarely causes trouble. In error messages, documentation, and code review, though, the difference matters. "The second parameter" refers to the signature. "The second argument" refers to a specific call.
How Binding Works
When a function is called, JavaScript creates fresh local variables for each parameter. It then assigns the arguments to them by position: the first argument goes to the first parameter, the second to the second, and so on. Names at the call site play no part in this.
Positional binding means that a variable's name at the call site does not matter:
function subtract(a, b) {
return a - b;
}
const b = 10;
const a = 3;
console.log(subtract(b, a)); // 7, because the outer b (10) binds to parameter aParameters Are Local Variables
Inside the function body, parameters behave like variables declared with let. You can read them, reassign them, and they disappear when the call ends. Reassigning a parameter never changes the caller's variable:
function shout(text) {
text = text.toUpperCase();
return text;
}
const word = "hi";
console.log(shout(word)); // "HI"
console.log(word); // "hi", unchangedCommon Mistakes
| Mistake | What actually happens |
|---|---|
| Passing arguments in the wrong order | Values bind to the wrong parameters silently. No error is thrown. |
| Expecting caller variable names to match parameters | Names are ignored. Only position counts. |
| Assuming JavaScript checks the argument count | It does not. The next chapter covers what happens with missing or extra arguments. |
When a function takes several parameters of the same type, such as two numbers or three strings, order mistakes become easy to make. A later chapter shows how options objects remove that risk.
Exercise
Given function ratio(top, bottom) { return top / bottom; } and the variables const bottom = 2; const top = 8;, what does ratio(bottom, top) return, and why?
Show answer
It returns 0.25. Arguments bind by position, so the outer bottom (2) becomes the parameter top and the outer top (8) becomes the parameter bottom, giving 2 / 8.
Exercise
In the call formatPrice(9.5, "EUR"), which are the parameters and which are the arguments?
Show answer
The parameters are amount and currency, the names in the function definition. The arguments are 9.5 and "EUR", the values passed in this particular call.