Argument vs Parameter
A parameter is a name listed in a function’s definition. An argument is the actual value you pass when you call the function. Parameters are placeholders; arguments fill them.
function greet(name, greeting) { // name, greeting → parameters
return `${greeting}, ${name}!`;
}
greet('Sam', 'Hello'); // 'Sam', 'Hello' → arguments
1. They don’t have to match in number
JavaScript doesn’t check the count.
function add(a, b) {
return a + b;
}
add(1); // NaN: b is undefined
add(1, 2, 3); // 3: the extra argument is ignored
2. Default parameters
A default applies when the argument is missing or undefined. null does not trigger it.
function page(n = 1) {
return n;
}
page(); // 1
page(undefined); // 1
page(null); // null
3. Rest parameters vs spread arguments
Same ... syntax, opposite jobs.
function sum(...nums) { // rest: collects arguments into an array
return nums.reduce((t, n) => t + n, 0);
}
const values = [1, 2, 3];
sum(...values); // spread: expands an array into arguments
More in rest vs spread.
4. The arguments object
Inside a regular function, arguments holds everything that was passed, whatever the parameter list says.
function count() {
return arguments.length;
}
count('a', 'b', 'c'); // 3
It’s array-like, not an array, and arrow functions don’t have one. Prefer rest parameters in new code.
5. fn.length counts parameters, not arguments
function f(a, b = 2, ...rest) {}
f.length; // 1: stops at the first default or rest parameter
((a, b) => {}).length; // 2
Libraries use this to implement things like currying.
6. How arguments are passed
Primitives are copied. Objects are passed as a copy of the reference: you can change the object’s contents, but reassigning the parameter doesn’t affect the caller.
function update(user) {
user.name = 'Changed'; // visible to the caller
user = { name: 'New' }; // only rebinds the local parameter
}
const u = { name: 'Original' };
update(u);
u.name; // 'Changed'
7. Named arguments with destructuring
JavaScript has no named arguments, so the convention is a single object parameter:
function createUser({ name, role = 'viewer', active = true } = {}) {
return { name, role, active };
}
createUser({ name: 'Sam', role: 'admin' });
Order stops mattering and call sites document themselves. The = {} lets you call it with no argument at all.
Summary
| Parameter | Argument | |
|---|---|---|
| Where | Function definition | Function call |
| What | A variable name | A value or expression |
| Count available as | fn.length |
arguments.length or rest.length |
| Missing one | The parameter is undefined (or its default) |
n/a |
| Collect or expand many | Rest: ...params |
Spread: ...array |
Follow-up questions to expect
- “Is JavaScript pass-by-value or pass-by-reference?” Pass-by-value, where the value for an object is a reference. Section 6 is the proof.
- “What’s the difference between
argumentsand rest parameters?” Rest is a real array, works in arrow functions, and only includes the extras you name. - “How would you make a parameter required?” Throw from a default:
function f(x = required('x')) {}, or use TypeScript.
