Parameters and arguments: what a call passes
A parameter is a name in the definition of a function, and an argument is a value in the call. The two lists are matched by position: the first argument goes to the first parameter, the second to the second, and the names themselves never travel. JavaScript does not compare the two lengths either, so a call may pass fewer values than the definition declares or more, and the function is left to deal with what it received.
This lesson walks along that edge. A missing argument arrives as undefined, an extra one is dropped, the old arguments object of a function holds everything the call passed, and a rest parameter collects the same values into a real array. Defaults, which fill a missing argument with a value instead of undefined, are the next lesson.
Parameters and arguments
The definition below names two parameters and the call supplies two arguments. The first value lands in greeting, the second in name, and the body builds a sentence from both. The same pair of strings in the other order gives another sentence, and the function has no way to know which one was meant: the order is the contract between the definition and the call.
function greet(greeting, name) {
return `${greeting}, ${name}!`;
}
console.log(greet('Hello', 'Ada'));
console.log(greet('Ada', 'Hello'));
console.log('declared parameters:', greet.length);
node greet.js
Hello, Ada!
Ada, Hello!
declared parameters: 2
The second line is nonsense and the program is happy to print it, because two strings fit two parameters. greet.length is the number of parameters the definition declares, a property every function carries, and it says nothing about how many arguments a particular call passed. A function of three or four positional arguments is where this style starts to hurt, and the lesson on options objects replaces it with names.
Missing and extra arguments
A call with too few arguments leaves the rest of the parameters undefined, and a call with too many evaluates every argument and hands the extra ones to nobody. Neither case throws: the function body sees the parameters it declared, and the values that are left over are only reachable through arguments or a rest parameter.
function greet(greeting, name) {
return `${greeting}, ${name}!`;
}
console.log(greet('Hello'));
console.log(greet('Hello', 'Ada', 'extra'));
function show(a, b, c) {
return `a=${a} b=${b} c=${c}`;
}
console.log(show(1, 2));
console.log(show(1, 2, 3, 4));
node counts.js
Hello, undefined!
Hello, Ada!
a=1 b=2 c=undefined
a=1 b=2 c=3
The first line is the one that catches a beginner: the call is not an error, it builds a sentence with the text undefined in it, and the bug travels on as a string that looks almost right. The third word of the second call is a string "extra" that the function never reads. The two show calls repeat the same two facts with numbers: a parameter with no argument holds undefined, and a fourth argument leaves no trace.
The arguments object
Every function written with the word function has a local binding called arguments, and it holds every value the call passed. It is array-like rather than an array: it has length and indexed elements, and it has no map, filter or join. The old code that needed those methods borrowed them from Array.prototype.
function collect() {
console.log('length:', arguments.length);
console.log('is an array:', Array.isArray(arguments));
console.log('joined:', Array.prototype.join.call(arguments, ' + '));
}
collect(1, 2, 3);
function outer() {
const inner = () => typeof arguments;
return inner();
}
console.log('an arrow inside a function sees:', outer(1, 2));
node arguments.js
length: 3
is an array: false
joined: 1 + 2 + 3
an arrow inside a function sees: object
The function declares no parameters at all and still received three values, which is the older way to write a function of unknown arity. The second line is why this object is not an array in disguise: the array checks fail on it. An arrow function has no arguments of its own, so the arrow in outer reads the one of the enclosing function, which is why arrows are not a way to ask a call how many arguments it had.
Rest parameters
A rest parameter is written with three dots before the name and stands last in the list. It collects every argument from that position on into a real array, so join, map and for...of work directly, and the body does not have to borrow anything. A function may declare parameters before it, and those take the first values as usual.
function sum(...numbers) {
let total = 0;
for (const number of numbers) {
total += number;
}
return total;
}
console.log('sum:', sum(1, 2, 3));
console.log('empty:', sum());
function head(first, ...rest) {
console.log('rest is an array:', Array.isArray(rest));
return `first=${first} rest=[${rest.join(', ')}]`;
}
console.log(head(1, 2, 3));
console.log('declared parameters before the rest:', head.length);
node rest.js
sum: 6
empty: 0
rest is an array: true
first=1 rest=[2, 3]
declared parameters before the rest: 1
A call with no arguments gives an empty array rather than undefined, which is why the loop of sum needs no guard. The last line is the quiet detail that matters later: length counts the parameters before the first default or rest, so head.length is 1 and not 2. The lesson on currying leans on exactly that number, and the rest parameter is also what the next lesson on defaults builds on.
Exercises
- Call greet from greet.js with no arguments at all. Which line appears, and what stands where the name should be?
- Add a third parameter to show in counts.js and call it with two arguments. What does the new parameter hold?
- Call collect with no arguments. What do length and the joined text become?
- Write a function whose only parameter is a rest parameter and call it with three values. What is its length, and why is it that number?
- Give first in rest.js a default of 0 and call head with no arguments. What is the value of rest then?
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What is the difference between a parameter and an argument?
Answer
A parameter is a name in the definition, an argument is a value in the call
The definition declares parameters and each call supplies arguments, which are matched by position.
-
What is a parameter worth when the call passes no value for it?
Answer
undefined
The parameter is bound to undefined, which is why a template string shows the word undefined.
-
What does this program print? ``
function f(a, b) { return${a}-${b}; } console.log(f(1));``Answer
1-undefined
The second parameter has no argument, so it is undefined and the template string writes that word.
-
What does this program print? ``
function f(a) { return${arguments.length}:${a}; } console.log(f(1, 2, 3));``Answer
3:1
arguments holds the three values of the call while the single parameter took the first of them.
-
What happens to an argument that no parameter of the function declares?
Answer
It is evaluated, and the declared parameters never see it
JavaScript does not check the count, so the value is passed and only arguments or a rest parameter can reach it.
-
What does this program print?
function f(...rest) { return Array.isArray(rest) + ":" + rest.join(","); } console.log(f(1, 2));Answer
true:1,2
A rest parameter is a real array, so the array check passes and join is its own method.
-
What is
Array.isArray(arguments)inside a function written with the wordfunction?Answer
false, because arguments is array-like and not an array
The object has length and indices and none of the array methods, which is why older code borrowed them.
-
Which function form has an
argumentsbinding of its own?Answer
A function declaration or expression, but not an arrow function
An arrow has no arguments of its own and reads the one of the enclosing function, so it cannot report its own call.
-
What does this program print?
function f(a, b) { return b; } console.log(f(1));Answer
undefined
The parameter b has no argument and holds undefined, which is the value the function returns.
-
What does
fn.lengthcount?Answer
The parameters before the first default or rest parameter
The property stops at the first parameter that has a default or takes the rest, so a rest parameter never counts.
The code on this page was run with Node.js 24.22.1 and linted with ESLint 10.14.0 on Alpine Linux 3.24.3 (x86_64) in a Docker container.
Article author: Arthur Isaev