Parameters and Arguments

Updated · 7 min read
easyES2026javascriptfunctionsparametersdefault-parametersrest-parameters

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.
  • arguments is the older way to see all arguments; use rest parameters instead.

Missing and Extra Arguments

mismatch.js▶ Try it
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));
Output
Navika, grade 9, Dallas
Srihitha, grade 11, undefined
Advitha, grade 9, San Francisco

Explanation: 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

syntax.js
function name(param = defaultValue) { ... }

Example

defaults.js▶ Try it
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));
Output
10
15
25
0

Explanation:

  • lateFee(5) uses both defaults: 5 * 2.
  • Passing undefined explicitly also triggers the default, which lets you skip a middle parameter.
  • Passing null does not: null is a real value, and 5 * null is 0. Defaults replace only undefined.

Defaults are evaluated at call time, left to right, so a default can use earlier parameters or call a function:

default-expressions.js▶ Try it
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));
Output
1/5/2026: $100 + $8 tax
1/5/2026: $100 + $0 tax

Rest Parameters

A rest parameter collects all remaining arguments into a real array. It is written with three dots and must be the last parameter:

rest.js▶ Try it
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);
Output
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:

options.js▶ Try it
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 }));
Output
Navika: 120 USD, due in 30 days, email off
Advitha: 85 USD, due in 30 days, email on

Each 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:

by-value.js▶ Try it
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);
Output
120
70

Explanation: 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:

arguments.js▶ Try it
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));
Output
false
250

arguments 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:

record-payment.js▶ Try it
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);
Output
450
250 { amount: 200, method: 'check', note: 'Fall term', date: '01/05/2026' }
card

The ...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
average.js▶ Try it
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());
Output
84
100
null

Quick check: Parameters

3 questions
1With function f(rate = 2), what is rate when you call f(null)?
2What does a rest parameter give you?
3A function changes student.balance on an object it received. What happens to the caller's object?

Key Takeaways

PatternCode
Defaultfunction f(x, rate = 2)
Restfunction f(first, ...others)
Optionsfunction f({ a, b = 1 } = {})
Spread into a callf(...array)

Was this tutorial helpful?

Lesson 28 of 139