Functions: naming and reusing behaviour · Lesson 16 of 77 · practice

Parameters, arguments and defaults

About 50 minutes.

By the end of this lesson you can

  • Use the words parameter and argument correctly, and know which is which.
  • Write a function that takes information from its caller and uses it in the body.
  • Predict what happens inside a function when the caller leaves an argument out.
  • Give a parameter a default value and say exactly when that default is used.
  • Explain why null does not trigger a default but a missing argument does.
  • Explain why changing a number parameter inside a function leaves the caller's variable alone.

A function that cannot be told anything

Explanation

The greet function from lesson one always says the same thing. It is a recipe with no blanks: useful exactly once, and useless the moment you want to greet somebody by name. To be worth keeping, a function usually needs the caller to hand it something to work with.

That is what the round brackets in the declaration are for. They looked like decoration in lesson one because they were empty. Put a name inside them and you have made a slot: a value the caller must supply, which the body can then use.

Parameter and argument: two words for two moments

Explanation

A parameter is the name you write inside the brackets of the declaration. It is a placeholder — a variable that exists only inside the function body, and that has no value at all until somebody calls the function.

An argument is the actual value you write inside the brackets of a call. It is real: a number, a string, a boolean, something that exists right now.

They pair up by position. The first argument fills the first parameter, the second fills the second, and so on. The names do not have to match and usually will not — the caller's variable might be called customerName while the parameter is just name, and that is fine, because the function only ever sees the value.

People use the two words loosely in conversation and you will survive either way, but the distinction is worth keeping in your head, because parameter and argument are exactly the two moments this whole module is built on: writing the recipe, and running it.

The form with blanks

An analogy

A parameter is a blank line on a printed form. The form says Dear ______, and the blank is part of the design; it is not a name, it is a place where a name goes.

An argument is the ink. Somebody writes Ada in the blank, and for that one filled-in copy the letter reads Dear Ada. Fill in a fresh copy with Grace and the original form is untouched. Every call gets its own copy of the form.

What happens when the caller leaves it out

Explanation

JavaScript does not complain when the number of arguments does not match the number of parameters. This surprises people coming from other languages, and it is worth meeting deliberately rather than discovering at three in the morning.

If the caller supplies fewer arguments than there are parameters, the leftover parameters are undefined. The function still runs, and the value undefined then flows through your arithmetic or your text and produces something strange rather than an error.

If the caller supplies more arguments than there are parameters, the extra ones are simply ignored. There is no warning about that either.

Defaults: a sensible value when none arrives

Explanation

You can give a parameter a default by writing an equals sign and a value after its name, like name = "friend". If the call supplies a value, that value is used. If it does not, the default is used instead, and the body never has to think about the missing case.

The rule for when a default kicks in is precise and worth memorising exactly, because a fuzzy version of it will bite you: the default is used when the argument is undefined. That covers the argument being left out entirely, and it also covers the caller passing undefined on purpose.

It does NOT cover null. It does not cover 0, or an empty string, or false. Those are all real values that somebody deliberately supplied, so JavaScript hands them straight to the body. If a caller passes null, the body gets null, defaults and all.

Defaults are worked out at the moment of the call, not once when the file loads, and a default may refer to a parameter written before it — so height = width is legal and means what it looks like it means.

Where this goes wrong

A common mistake

Putting the optional parameter first. Arguments pair up by position, so if the first parameter has a default and the second does not, a caller who wants to supply only the second one has to pass undefined explicitly to skip the first. Put the parameters the caller always supplies at the front.

Assuming null means missing. Data that came from somewhere else — a form, a file, another programmer — often uses null for empty. A default will not catch it, and you will see the word null appear in the middle of your output. Lesson four of module seven deals with checking input properly; for now, just know that null arrives intact.

Silently accepting the wrong number of arguments. Because JavaScript never complains, a typo like greet("Ada", "Hello") on a function declared as greet(greeting, name) produces confident nonsense rather than an error. Read the declaration before you call it.

The function gets the value, not your variable

A mental model

When you pass a number, a string or a boolean, the function receives a copy of the value. Assigning to the parameter inside the body changes only the function's own copy; the caller's variable is untouched. The final worked example below shows this happening.

This is the sort of fact that sounds like trivia until it saves you an hour. A function cannot reach back through its own brackets and edit the caller's variables, so the only way a function can tell its caller anything is by handing a value back — which is lesson three.

The picture is slightly different for arrays and objects, which module four introduces. Do not worry about that yet; for the values you have met so far, a copy is exactly what arrives.

MDN — Functions (JavaScript Guide), function parameters

Check there: The guide states that parameters are passed to functions by value, so reassigning a parameter inside a function is not visible to the caller.

Recap

Recap

Parameters are the named blanks in a declaration; arguments are the values written into them at the call, matched up by position. A missing argument leaves its parameter undefined and extra arguments are ignored — neither is an error. A default fills in for an argument that is undefined, and only for undefined: null, 0, false and the empty string all arrive as themselves.

Worked examples

Every output below was produced by actually running the code in the same sandbox your exercises run in — it is not a prediction.

One parameter, two callers

The same body, told something different each time. Notice that the body never mentions Ada or Grace: it only mentions name, and the calls decide what name is.

1function greet(name) {2  console.log("Hello, " + name + "!");3}4 5greet("Ada");6greet("Grace");
  • line 1

    name is the parameter: a blank in the recipe. It exists only inside this function.

  • line 2

    The body joins three strings with +. Whatever the caller supplied sits in the middle.

  • line 5

    "Ada" is the argument. For the duration of this call, and only this call, name holds "Ada".

  • line 6

    A second call with a different argument. The declaration was not touched between the two.

What it prints

Hello, Ada!Hello, Grace!

What a forgotten argument looks like

Exactly the same function, called with nothing. This is the output you will one day see in a real program, so it is worth recognising on sight rather than deducing under pressure.

1function greet(name) {2  console.log("Hello, " + name + "!");3}4 5greet();
  • line 5

    No argument, and no error. name is undefined inside the body, and joining undefined to a string produces the word undefined in the output.

What it prints

Hello, undefined!

When the default is used, and when it is not

The precise rule, demonstrated. Four calls, four outcomes: a supplied value, a missing argument, an explicit undefined, and a null. Only two of them use the default.

1function greet(name = "friend") {2  console.log("Hello, " + name + "!");3}4 5greet("Ada");6greet();7greet(undefined);8greet(null);
  • line 1

    The default: if the argument is undefined, name becomes "friend".

  • line 5

    A real value is supplied, so the default is ignored.

  • line 6

    No argument at all, so name is undefined, so the default applies.

  • line 7

    Passing undefined on purpose is treated exactly like leaving it out. This is the rule stated literally.

  • line 8

    null is a real value that the caller chose, so it is NOT replaced by the default. It lands in the output as the word null.

What it prints

Hello, Ada!Hello, friend!Hello, friend!Hello, null!

Several parameters, and one argument too many

Position is everything. Watch how supplying one, two and three arguments fills the parameters left to right, and what JavaScript does with a fourth argument that has nowhere to go.

1function order(item, quantity = 1, note = "no note") {2  console.log(quantity + " x " + item + " (" + note + ")");3}4 5order("coffee");6order("coffee", 3);7order("coffee", 3, "extra hot");8order("coffee", 3, "extra hot", "ignored");
  • line 1

    item is required; quantity and note have defaults. The parameters with defaults come last, which is what lets a caller supply only the first.

  • line 5

    One argument, so both defaults apply.

  • line 6

    Two arguments: quantity is supplied, note still defaults.

  • line 8

    A fourth argument has no parameter to land in, so it is silently ignored. No error, no warning — the output is identical to line 7.

What it prints

1 x coffee (no note)3 x coffee (no note)3 x coffee (extra hot)3 x coffee (extra hot)

Changing a parameter does not change the caller's variable

The function receives a copy of the number, not the variable itself. Both lines of output are printed by the same program, and they disagree on purpose.

1function addOne(number) {2  number = number + 1;3  console.log("inside: " + number);4}5 6let score = 10;7addOne(score);8console.log("outside: " + score);
  • line 2

    This assigns to the parameter, which is the function's own local copy.

  • line 7

    The value 10 is copied into the parameter. The variable score is not handed over and cannot be reached from in here.

  • line 8

    score is still 10. If you want the new number, the function has to hand it back — that is lesson three.

What it prints

inside: 11outside: 10

Check yourself

Not scored, not stored. Getting one wrong is the useful part.

A program declares function tax(amount) and later calls tax(40). Which of these is the argument?

A default can refer to a parameter declared before it. What does this print?

function box(width, height = width) {  console.log(width * height);}box(3);box(3, 4);

Given function greet(name = "friend"), which call uses the default value "friend"?

Write it yourself

Exercise · core · 5 tests

A greeting with two defaults

Change logGreeting so that both of its parameters have defaults. If the caller does not supply a name, use "friend". If the caller does not supply a greeting, use "Hello". The printed line is the greeting, then a comma and a space, then the name, then an exclamation mark — for example: Hello, Ada! One of the tests calls logGreeting(null). Think about what should happen there before you run it: null is a value, not a missing argument.

What your code must do

logGreeting(name, greeting) prints exactly one line: greeting + ", " + name + "!". A missing name becomes "friend" and a missing greeting becomes "Hello". Passing undefined counts as missing. Passing null does NOT: the word null appears in the output, which is correct behaviour here, not a bug to work around.

Define logGreeting at the top level of your code — the tests call it by name.

Runs on your device in a sandbox with no network and no access to this page.

Worth remembering

  • Parameter or argument: which one is written in the declaration?

    The parameter. It is a placeholder that exists only inside the body. The argument is the real value written at the call, matched to parameters by position.

  • Exactly when is a default parameter used?

    Only when the argument is undefined — left out entirely, or passed as undefined on purpose. null, 0, false and the empty string are real values and are used as-is.

Check this lesson against the source

We wrote the explanation; we did not invent the facts. This is the page that backs them.

MDN — Default parameters (JavaScript reference)

Check there: The page states that default parameters are used when the argument is undefined — including when it is omitted or passed explicitly as undefined — and that defaults are evaluated at call time and may refer to earlier parameters.

MDN — Functions (JavaScript reference)

Check there: The reference overview of parameters, including rest parameters.

MDN — null

Check there: That null is a value meaning the deliberate absence of an object value, distinct from undefined.

Prefer to read the source in a structured breakdown? Turn this doc into a breakdown

0 of 1 exercise in this lesson solved on this device

Consent version 2026-07-31.1

Cookie preferences