Skip to content
Tutorials Center
My learning

Topics

Lesson 30 of 156 ยท Functions

Parameters and Arguments

Pass data into JavaScript functions. Learn default parameters, rest parameters, the arguments object, options objects and how objects are passed.

In the Functions lesson you saw that a function can take inputs. The names in the function definition are parameters; the values you pass in a call are arguments. They're the doorway through which data gets into your function, and JavaScript is unusually relaxed about them: it never checks that you passed the "right" number of arguments.

That flexibility is handy, but it also causes bugs. This lesson shows exactly what happens with missing and extra arguments, then covers the modern tools for handling them well: default parameters, rest parameters and options objects. You'll also meet the older arguments object and learn an important detail about what happens when you pass an object to a function.

Parameters are local variables

When you call a function, each parameter becomes a local variable inside the function, holding the matching argument. Arguments are matched by position: the first argument goes to the first parameter, the second to the second, and so on.

JS Matching by position

Press Run (or Ctrl + Enter) to see the console output here.

No output.

That run shows JavaScript's two rules:

  • Too few arguments: the missing parameters are undefined. No error.
  • Too many arguments: the extras are ignored by the parameters. No error either.

So it's your job to make functions behave sensibly when arguments are missing. Default parameters make that easy.

Default parameters

Write = value after a parameter to give it a default. The default is used when the argument is missing or explicitly undefined:

JS Default parameters

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Notice the last two calls. Only undefined triggers a default. null, 0, "" and false are real values, so they're used as given. That's usually what you want: a count of 0 is a valid count, not a missing one.

The old way. Before defaults existed (they arrived with ES2015), code often wrote name = name || "friend"; inside the function. That replaces every falsy value, including 0 and "", which causes bugs. You'll still see it in older code; prefer default parameters.

Defaults can be expressions

A default isn't limited to a fixed value. It can be any expression, and it's evaluated fresh on every call that needs it. It can even use earlier parameters:

JS Expression defaults

Press Run (or Ctrl + Enter) to see the console output here.

No output.

A default can only refer to parameters that come before it. Put parameters with defaults at the end of the list, so callers can leave them off; a default in the middle forces callers to pass undefined to skip it.

Rest parameters

Sometimes a function should accept any number of arguments, like Math.max(3, 9, 4). A rest parameter, written with three dots, collects all the remaining arguments into a real array:

JS Rest parameters

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The rest parameter must be last, since it takes "everything else", and a function can have only one. Because it's a genuine array, you can use length, for...of and all the array methods on it.

The same three dots also appear when calling a function. There, they're the spread syntax, which does the opposite: it unpacks an array into separate arguments. sum(...[1, 2, 3]) is the same as sum(1, 2, 3). Spread and Rest covers both in depth.

The arguments object

Before rest parameters existed, functions used a special variable called arguments. It's available inside every regular (non-arrow) function and holds all the arguments that were passed:

JavaScript
function oldSum() {
  let total = 0;
  for (let i = 0; i < arguments.length; i++) {
    total += arguments[i];
  }
  return total;
}

oldSum(1, 2, 3); // 6

You'll see it in older code, but avoid it in new code:

  • It's array-like, not an array: it has length and indexes, but no map, filter or other array methods.
  • Arrow functions don't have their own arguments.
  • The function signature function oldSum() hides the fact that it takes arguments at all. function sum(...numbers) documents itself.

Options objects: named arguments

Once a function has more than two or three parameters, calls become hard to read. What do the values in createUser("sam", true, false, 30) mean? Swap two booleans and nothing complains.

The common fix is to accept one object and name each setting as a property. Combine it with destructuring in the parameter list, and each property gets its own default:

JS An options object

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Read the parameter as: "take an object, pull out these properties, and use these defaults for any that are missing". The final = {} is the default for the whole object; without it, createUser() would crash, because you can't destructure undefined. Destructuring explains the syntax fully. Options objects are everywhere in real APIs; fetch(url, { method: "POST" }) and addEventListener("click", fn, { once: true }) both use them.

Passing objects: a shared reference

When you pass a primitive value like a number or string, the function gets its own copy. Reassigning the parameter has no effect outside. Objects and arrays behave differently: the function receives a reference to the same object. If the function changes the object, the caller sees the change:

JS Mutating vs reassigning

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The rule: reassigning a parameter only affects the local variable, but mutating an object through it affects the caller's object. Changing the caller's data by surprise is a common source of bugs. When a function needs a modified version, it's usually safer to build and return a new object, for example with spread: return { ...post, tags: [...post.tags, "featured"] };. Copying Objects goes deeper, and Pass by Value and Pass by Sharing explains the model behind it.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What does function f(a, b) { return b; } return for the call f(1)?

    Missing arguments aren't an error; the matching parameter is simply undefined.
  2. 2. Given function g(x = 5) { return x; }, what does g(null) return?

    Only undefined (or a missing argument) triggers a default. null is a real value, so it's used.
  3. 3. What does a rest parameter like ...items contain?

    A rest parameter gathers the remaining arguments into a genuine array, so all array methods work on it.
  4. 4. A function receives an array and calls .push() on it. What happens to the caller's array?

    Objects and arrays are passed as references to the same value, so mutations inside the function are visible outside.
  5. 5. Why add = {} in function setup({ debug = false } = {})?

    Without a default for the whole object, calling setup() would try to destructure undefined and throw a TypeError.

Summary

  • Arguments match parameters by position. Missing ones become undefined; extra ones are ignored.
  • Default parameters (x = 1) apply when the argument is missing or undefined, never for null, 0 or "". Defaults are evaluated on each call and can use earlier parameters.
  • A rest parameter (...items) collects the remaining arguments into a real array. It must come last.
  • The old arguments object is array-like and missing in arrow functions; prefer rest parameters.
  • For functions with many settings, accept an options object and destructure it with defaults, plus = {}.
  • Functions receive objects by reference: mutating them affects the caller, reassigning the parameter doesn't.

Next: Return Values โ€” getting results back out of your functions.