Default Parameters

The full text of this chapter, to read or revisit. To practise, do the exercises.

▶ Do the exercises

Exercise 1 — A Value When Nothing Is Passed

Remember: a parameter without an argument is `undefined`. That gives awkward messages:

function greet(name) { console.log("Welcome, " + name + "!"); } greet("Sam"); greet();

A default parameter fixes this: write `= value` after the parameter name. That value is used whenever no argument is passed.

function greet(name = "trainer") { console.log("Welcome, " + name + "!"); } greet("Sam"); greet();

Exercise 2 — Defaults for Numbers

Defaults are handy for optional settings. Here a potion heals 20 HP unless you say otherwise:

function heal(hp, amount = 20) { return hp + amount; } console.log(heal(50)); console.log(heal(50, 35));

Defaults work in every kind of function: declarations, expressions and arrow functions.

const attack = (damage = 10) => "Deals " + damage + " damage"; console.log(attack()); console.log(attack(45));

Exercise 3 — Quiz: Default Used?

What does this code print?

function level(n = 1) { return "Level " + n; } console.log(level()); console.log(level(7));
Options
• Level 1 then Level 7
• Level undefined then Level 7
• Level 1 twice
• An error

Hint : The default only steps in when nothing is passed.

Exercise 4 — Default Damage

Give `damage` a default of 15, so that the first call prints 15 and the second prints 40.

Hint : A default is written like an assignment, right in the parameter list.

Exercise 5 — undefined Triggers the Default

Precisely, the default is used when the argument is `undefined`: missing, or explicitly `undefined`. Any other value, even `0`, `""` or `null`, is kept as is.

function show(value = "default") { console.log(value); } show(); show(undefined); show(0); show(""); show(null);

The third line prints `0` and the fourth prints an empty line: those are real values, not "nothing".

Exercise 6 — Quiz: Zero Is a Value

What does this code print?

function bonus(points = 50) { return points; } console.log(bonus(0));
Options
• 0
• 50
• undefined
• An error

Hint : Is 0 the same thing as "nothing was passed"?

Exercise 7 — Order Matters

Arguments are matched by position, so put parameters with defaults last. Otherwise you can't skip them:

function introduce(name, type = "unknown", level = 1) { console.log(name + " | " + type + " | level " + level); } introduce("Zap"); introduce("Zap", "electric"); introduce("Zap", "electric", 5);

A default can even be computed from a previous parameter:

function stats(level, hp = level * 10) { console.log("Level " + level + ", " + hp + " HP"); } stats(4); stats(4, 99);

Exercise 8 — Optional Level

Complete the parameters so the level defaults to 1 and the two calls print `Zap Lv.1` and `Metachu Lv.12`.

Hint : The required parameter comes first, and the optional one carries its default.

Exercise 9 — Default Cheat Sheet

`function f(a, b = 5)` is called in different ways. Match each call with the value of `b`.

Items to match
• f(1)
• f(1, 8)
• f(1, undefined)
• f(1, 0)
Definitions
• 5
• 8
• 5
• 0

Exercise 10 — Flexible Potion

Write a `usePotion` function that heals a Codémon, with sensible defaults so most calls stay short.

Step 1: `usePotion(name, hp, amount = 20, max = 100)` returns the new HP: the current HP plus the amount, but never above the max. Step 2: it also prints `"name: X -> Y HP"` where X is the HP before and Y the HP after. Step 3: make the three calls shown below, using the defaults whenever possible.

Calls: Zap at 50 HP with a standard potion; Pyrochu at 90 HP with a standard potion; Metachu at 100 HP with a 50 HP potion and a max of 140.

Expected output:

Zap: 50 -> 70 HP Pyrochu: 90 -> 100 HP Metachu: 100 -> 140 HP
Starter code
// Write your function here // Make the three calls here
Back to the course