Sub topic 1
Parameters and Arguments
Every function call in JavaScript has two parts. The function definition lists names it expects to receive. The call site supplies actual values. These two parts have different names, and keeping them apart makes the rest of this tutorial easier to follow. This chapter sets out the vocabulary and the model used in later chapters: how values move from a call into a function, in what order, and where they live once inside.
Prerequisites
This chapter assumes you already know:
- Basic JavaScript syntax, including declaring variables with
letandconst - How to declare a function and call it
- How to work with arrays and objects
- Template literals (strings written with backticks and
${...})
You do not need to know anything about default values, rest parameters, or destructuring yet. Later chapters cover those.
The Two Terms
A parameter is a name listed in a function's definition. An argument is the value passed in when the function is called.
function greet(name, greeting) { // name and greeting are parameters
return `${greeting}, ${name}!`;
}
const message = greet("Ana", "Hello"); // "Ana" and "Hello" are arguments
console.log(message); // "Hello, Ana!"Here are the two terms side by side:
| Aspect | Parameter | Argument |
|---|---|---|
| Where it appears | In the function definition | In the function call |
| What it is | A local variable name | A value (the result of an expression) |
| When it exists | Created fresh on each call | Evaluated just before the call |
| Example above | `name`, `greeting` | `"Ana"`, `"Hello"` |
The ECMAScript specification and MDN both use these terms this way. Some older books call parameters "formal parameters" and arguments "actual parameters". The meaning is the same.
Binding by Position
JavaScript matches arguments to parameters by position. The first argument goes to the first parameter, the second to the second, and so on. Names at the call site make no difference.
function divide(dividend, divisor) {
return dividend / divisor;
}
const divisor = 2;
const dividend = 10;
console.log(divide(dividend, divisor)); // 5
console.log(divide(divisor, dividend)); // 0.2, the positions swappedThe variables at the call site happen to share names with the parameters, but JavaScript ignores that. Only order counts. Swapping arguments of the same type is a common bug, because nothing warns you about it. Functions with three or more parameters of similar types make this bug especially easy. The chapter on options objects shows a way to pass values by name.
Arguments Are Expressions, Evaluated First
An argument can be any expression: a literal, a variable, a calculation, or another function call. JavaScript evaluates all argument expressions left to right, and does so before the function body starts.
function trace(label, value) {
console.log(`evaluating ${label}`);
return value;
}
function add(a, b) {
console.log("body runs");
return a + b;
}
const total = add(trace("first", 1), trace("second", 2));
console.log(total);
// evaluating first
// evaluating second
// body runs
// 3This order has two consequences:
- The function only ever receives finished values. It cannot tell whether
3came from a literal, a variable, or1 + 2. - If evaluating an argument throws an error, the function is never entered.
Parameters Are Local Variables
Inside the function, each parameter behaves like a variable declared at the top of the body. It is created fresh on every call and disappears when the call ends, unless a closure keeps it alive. A later chapter covers closures. You can read a parameter and reassign it.
function countdown(n) {
while (n > 0) {
console.log(n);
n = n - 1; // reassigning the parameter is allowed
}
return "done";
}
const start = 3;
console.log(countdown(start)); // 3, 2, 1, then "done"
console.log(start); // 3, the caller's variable is unchangedReassigning n changes only the local binding. The caller's start stays 3. Objects follow the same rule with one exception: mutating an object through a parameter is visible to the caller. The chapter on passing primitives and objects explains why.
Many style guides discourage reassigning parameters, for example ESLint's no-param-reassign rule. Code is easier to read when a parameter always holds what the caller passed. If you need a working copy, put it in a new local variable.
Shadowing
A parameter with the same name as an outer variable shadows it. Inside the function, the name refers to the parameter.
const name = "outer";
function show(name) {
return name;
}
console.log(show("inner")); // "inner"
console.log(name); // "outer"Shadowing is legal, but it can confuse readers. It also means the function cannot reach the outer variable by that name.
Duplicate Parameter Names
Sloppy (non-strict) mode allows the same parameter name twice in an ordinary function. The last one wins. Strict mode rejects this with a SyntaxError. So do modules and classes, which are always strict. Arrow functions reject duplicates in every mode, and so do functions that use defaults, rest, or destructuring.
function sloppy(x, x) {
return x;
}
console.log(sloppy(1, 2)); // 2, in non-strict scripts only
// "use strict";
// function strict(x, x) {} // SyntaxError: Duplicate parameter nameTreat duplicate names as a bug. In practice, you get the error automatically in any ES module.
Count Mismatches Are Allowed
JavaScript does not check that the number of arguments matches the number of parameters. A call with too few or too many arguments still runs.
function pair(a, b) {
return [a, b];
}
console.log(pair(1)); // [1, undefined]
console.log(pair(1, 2, 3)); // [1, 2]Exactly how missing and extra arguments are handled is covered in the next chapter. For now, remember that JavaScript checks neither the count nor the types of arguments. If a function has requirements, the function itself has to enforce them.
Common Errors
| Mistake | Symptom | Fix |
|---|---|---|
| Swapping arguments of the same type | Wrong result, no error | Check the parameter order, or use an options object |
| Expecting names at the call site to match parameters | Values land in the wrong place | Remember that binding is by position only |
| Reassigning a parameter and expecting the caller to see it | Caller's variable unchanged | Return the new value instead |
| Shadowing an outer variable by accident | Outer value unreachable | Rename the parameter |
Exercise
Given function sub(a, b) { return a - b; }, let x = 1;, what does sub(x++, x++) return, and what is x afterward?
Show answer
It returns -1 and x is 3. Arguments are evaluated left to right before the body runs. The first x++ yields 1 and sets x to 2. The second yields 2 and sets x to 3. So a = 1, b = 2, and 1 - 2 = -1.
Exercise
In function f(value) { value = value * 2; return value; }, after const n = 5; f(n);, what is n, and why?
Show answer
n is still 5. The parameter value is a separate local binding that receives a copy of the number 5. Reassigning it does not affect the caller's variable n.