Parameters and Arguments
Parameters are the variable names listed in a function's definition; arguments are the actual values passed when the function is called. JavaScript is relaxed about matching them up: you can pass fewer arguments than there are parameters, or more, and nothing complains. Modern JavaScript adds default parameters and rest parameters to handle both cases cleanly, and an options object to make calls with many settings readable.
In One Minute
- A missing argument makes its parameter
undefined. Give it a fallback with a default:function lateFee(days, rate = 2). - Extra arguments are ignored, unless you collect them with a rest parameter:
function sum(...amounts). - For more than two or three settings, accept one options object and destructure it:
function bill({ name, amount, currency = "USD" }). - Arguments are passed by value, but an object's value is a reference: a function can change an object's properties.
argumentsis the older way to see all arguments; use rest parameters instead.
Missing and Extra Arguments
function describe(name, grade, city) {
return `${name}, grade ${grade}, ${city}`;
}
console.log(describe("Navika", 9, "Dallas"));
console.log(describe("Srihitha", 11));
console.log(describe("Advitha", 9, "San Francisco", "extra", 42));Navika, grade 9, Dallas
Srihitha, grade 11, undefined
Advitha, grade 9, San FranciscoExplanation: the second call passes no city, so city is undefined. The third passes two extra arguments, which are silently ignored. JavaScript never checks the count; that's your job (or TypeScript's).
Default Parameters
A default parameter gives a parameter a value to use when the argument is missing or undefined.
Syntax
function name(param = defaultValue) { ... }Example
function lateFee(daysLate, ratePerDay = 2, cap = 30) {
return Math.min(daysLate * ratePerDay, cap);
}
console.log(lateFee(5));
console.log(lateFee(5, 3));
console.log(lateFee(20, undefined, 25));
console.log(lateFee(5, null));10
15
25
0Explanation:
lateFee(5)uses both defaults:5 * 2.- Passing
undefinedexplicitly also triggers the default, which lets you skip a middle parameter. - Passing
nulldoes not:nullis a real value, and5 * nullis0. Defaults replace onlyundefined.
Defaults are evaluated at call time, left to right, so a default can use earlier parameters or call a function:
function invoice(amount, tax = amount * 0.08, issued = new Date("2026-01-05T10:00:00").toLocaleDateString("en-US")) {
return `${issued}: $${amount} + $${tax} tax`;
}
console.log(invoice(100));
console.log(invoice(100, 0));1/5/2026: $100 + $8 tax
1/5/2026: $100 + $0 taxRest Parameters
A rest parameter collects all remaining arguments into a real array. It is written with three dots and must be the last parameter:
function total(...amounts) {
return amounts.reduce((sum, a) => sum + a, 0);
}
console.log(total(120, 85, 45));
console.log(total(10));
console.log(total());
function logPayments(name, ...amounts) {
console.log(`${name} made ${amounts.length} payments:`, amounts);
}
logPayments("Srihitha", 200, 150, 100);250
10
0
Srihitha made 3 payments: [200, 150, 100]The rest parameter is always an array, empty if there are no extra arguments, so array methods work on it right away. The same three dots in a call do the opposite: total(...fees) spreads an array into separate arguments. Spread syntax has its own lesson in the arrays chapter.
Options Objects: Named Arguments
When a function takes many settings, positional calls become a guessing game: what does createInvoice("Navika", 120, true, false, 30) mean? The common solution is to accept a single object and destructure it in the parameter list, with defaults:
function createInvoice({ name, amount, currency = "USD", dueInDays = 30, sendEmail = false }) {
return `${name}: ${amount} ${currency}, due in ${dueInDays} days, email ${sendEmail ? "on" : "off"}`;
}
console.log(createInvoice({ name: "Navika", amount: 120 }));
console.log(createInvoice({ amount: 85, name: "Advitha", sendEmail: true }));Navika: 120 USD, due in 30 days, email off
Advitha: 85 USD, due in 30 days, email onEach call now reads like a sentence, the order of settings doesn't matter, and new options can be added later without breaking existing calls.
How Arguments Are Passed
JavaScript passes every argument by value: the parameter gets a copy of the argument's value. For primitives (numbers, strings, booleans), that means the function can't affect the caller's variable. For objects, the value being copied is a reference to the object, so the function and the caller share the same object:
function addPayment(balance, student) {
balance -= 50; // changes the local copy only
student.balance -= 50; // changes the shared object
}
let balance = 120;
const student = { name: "Navika", balance: 120 };
addPayment(balance, student);
console.log(balance);
console.log(student.balance);120
70Explanation: the number 120 was copied into the parameter, so changing it inside the function had no effect outside. The object wasn't copied; only the reference was, and both names point at the same object, so its balance changed. Changing an argument object inside a function is called a side effect, and it often surprises callers. Returning a new object is usually clearer; the lesson on copying objects shows how.
The arguments Object (Older Way)
Before rest parameters existed (ES2015), every regular function had an arguments object holding all the arguments passed:
function oldTotal() {
let sum = 0;
for (let i = 0; i < arguments.length; i++) {
sum += arguments[i];
}
console.log(Array.isArray(arguments));
return sum;
}
console.log(oldTotal(120, 85, 45));false
250arguments looks like an array but isn't one: it has length and indexes, but no map, filter or reduce. Arrow functions don't have their own arguments at all. You'll meet it in older code and some platform scripts; in new code, use ...rest.
In Practice
The Fee Tracker's recordPayment takes required values as parameters and optional ones as an options object with defaults. It returns a new record instead of changing the one passed in:
function recordPayment(student, amount, { method = "card", note = "", date = "01/05/2026" } = {}) {
if (!(amount > 0)) throw new RangeError(`Invalid amount: ${amount}`);
return {
...student,
balance: student.balance - amount,
lastPayment: { amount, method, note, date },
};
}
const navika = { name: "Navika", balance: 450 };
const updated = recordPayment(navika, 200, { method: "check", note: "Fall term" });
console.log(navika.balance);
console.log(updated.balance, updated.lastPayment);
console.log(recordPayment(navika, 50).lastPayment.method);450
250 { amount: 200, method: 'check', note: 'Fall term', date: '01/05/2026' }
cardThe ...student spread copies the student's properties into a new object; the objects chapter covers it.
Common Mistakes
Expecting null to trigger a default. Only undefined does. Use value ?? fallback inside the function if null should count as missing.
A rest parameter that isn't last. function f(...a, b) is a SyntaxError.
Destructuring an options parameter with no default. Calling with no argument throws. Add = {}.
Changing an argument object by accident. Callers don't expect it. Return a new object instead.
Practice
Challenge: Write average(...scores) that returns the average of any number of scores rounded to one decimal, and returns null when called with no scores.
Show solution
function average(...scores) {
if (scores.length === 0) return null;
const sum = scores.reduce((total, s) => total + s, 0);
return Math.round((sum / scores.length) * 10) / 10;
}
console.log(average(90, 85, 77));
console.log(average(100));
console.log(average());84
100
nullKey Takeaways
| Pattern | Code |
|---|---|
| Default | function f(x, rate = 2) |
| Rest | function f(first, ...others) |
| Options | function f({ a, b = 1 } = {}) |
| Spread into a call | f(...array) |
Was this tutorial helpful?