Sub topic 2
What Happens with Missing or Extra Arguments
This chapter assumes you can declare and call functions, use let and const, and read template literals. It builds on the previous chapter's distinction: parameters are the names in a function's definition, and arguments are the values you pass when you call it.
JavaScript Does Not Check Argument Counts
Many languages reject a call whose argument count doesn't match the function's parameter list. JavaScript accepts it. Any function can be called with fewer arguments than it has parameters, or with more, and no error is raised.
Binding happens by position. The first argument goes to the first parameter, the second to the second, and so on. Two rules handle a count mismatch:
- A parameter with no matching argument gets the value
undefined. - An argument with no matching parameter has no name inside the function.
function describe(a, b) {
return `a=${a}, b=${b}`;
}
console.log(describe(1)); // "a=1, b=undefined"
console.log(describe(1, 2, 3)); // "a=1, b=2" (the extra argument is ignored)
console.log(describe()); // "a=undefined, b=undefined"Common Errors This Causes
Because a missing argument fails silently, the bug shows up later as a strange value instead of a clear error.
| Situation | What you see |
|---|---|
| Missing number used in math | `NaN` |
| Missing string in a template | the text `"undefined"` |
| Property read on a missing object | `TypeError: Cannot read properties of undefined` |
| Extra argument passed by mistake | nothing, the value is quietly dropped |
function total(price, quantity) {
return price * quantity;
}
console.log(total(10)); // NaN, because 10 * undefined is NaN
function greetUser(user) {
return `Hi, ${user.name}`;
}
try {
greetUser();
} catch (err) {
console.log(err instanceof TypeError); // true
}Extra Arguments Are Not Lost Entirely
An extra argument has no parameter name, but the call still received it. Later chapters cover the two ways to reach those values: the legacy arguments object and rest parameters. Without one of them, extra arguments are unreachable.
Ignored extras also explain a well-known surprise with callbacks. Array.prototype.map passes three arguments to its callback: the element, the index, and the array. A function with two parameters will treat the index as its second argument:
console.log(["10", "10", "10"].map(parseInt)); // [10, NaN, 2]Here parseInt receives the index as its radix argument. Index 0 means "use the default radix", radix 1 is invalid, and radix 2 parses "10" as binary.
Checking for Missing Values
To guard against a missing argument by hand, compare the parameter to undefined:
function scale(value, factor) {
if (factor === undefined) {
factor = 1;
}
return value * factor;
}
console.log(scale(5)); // 5
console.log(scale(5, 3)); // 15This pattern works, but it is verbose. The next chapter replaces it with default parameter values.
Exercise
Given function pair(x, y, z) { return [x, y, z]; }, what does pair(1, 2) return, and what does pair(1, 2, 3, 4) return?
Show answer
pair(1, 2) returns [1, 2, undefined] because z has no matching argument. pair(1, 2, 3, 4) returns [1, 2, 3] because the fourth argument has no parameter and is ignored.
Exercise
Why does total(10) in this chapter return NaN and not throw an error?
Show answer
The missing quantity parameter is undefined. Multiplying a number by undefined gives NaN, and JavaScript never raises an error for a wrong argument count.