JS Sketchbooksee JavaScript think ✏️
← back to phase 3

3.10 — Default parameters & pure functions

Three finishing touches for your function-building skills. First, a fix for an old wound: in 3.2, a missing argument left undefined in the parameter and the function shouted nonsense. Today the parameter gets a penciled-in plan B — a default value that slides in whenever nothing arrives.

Second, the opposite problem: what if you don’t know how many arguments are coming? A rest parameter (...numbers) gathers ANY number of incoming values into one real array, so the function body can work with them as a group.

Third, a distinction your future employer cares about deeply: some functions are pure — sealed boxes where the same inputs always produce the same returned output, touching nothing else — and some reach outside themselves and change things. Both are legal. But one of them is a dream to test, and testing is where you’re headed.

watch it happen
function greet(name = "friend") {
  console.log("Hello, " + name + "!");
}

greet();
greet("Vasavi");

One new mark in the definition: name = "friend". That’s a DEFAULT PARAMETER — a fallback penciled into the slot itself. It does nothing while arguments arrive normally; it waits for the day one doesn’t.

greetnameemptypenciled-in fallback: "friend"a parameter with a plan B
console(nothing logged yet)
under the hood

The deeper story, with the real names for things — this part is what turns “I saw it” into “I can explain it.”

Default parameters, precisely: name = "friend" evaluates the fallback only when the incoming argument is undefined — absent entirely, or explicitly passed as undefined. (Passing null does NOT trigger it — null is a deliberate value, as lesson 1.7 taught.)

Defaults can even use earlier parameters: function area(width, height = width) is legal and genuinely useful.

A rest parameter, precisely: ...numbers must be the LAST parameter. It always gathers into a genuine array — .reduce, .map and friends all work on it — never a fixed count of separate slots. It is how functions like Math.max(...) accept any number of arguments at all.

A pure function satisfies two promises: same inputs → same output, and no side effects — no changing outer variables, no printing, no saving, no network. Anything that breaks either promise makes the function impure.

Note the fine print: even console.log is technically a side effect — the world (your console) changed. That’s why well-factored code computes with pure functions and keeps the printing at the edges.

Fun fact: the world’s most used “functional programming language” is… Excel. Every spreadsheet cell is a little pure function of other cells: same inputs, same output, no side effects. That is exactly why a billion people trust it with their money without ever debugging it.

your turn

⌨️ a parameter with a plan B

An area function where the second side is optional — leave it out and you get a square. Defaults meet return.

requirements:

  • A function named area with two parameters: width, and height whose default value is width itself.
  • It RETURNS width × height — no printing inside the function (keep it pure!).
  • Print area(3) (a 3×3 square — the default kicks in), then area(2, 5).

when you press RUN, the console must show exactly:

9
10

✏️ Quick check 1

Type exactly what greet() — no argument — prints now that the default exists:

✏️ Quick check 2

Same sum(...numbers) function. Type what sum(10, 20, 30) returns:

✏️ Quick check 3

add(2, 3) returns 5 today. Type what it returns if you call it again in five years:

✏️ Quick check 4

addToTotal changes a variable OUTSIDE itself. Functions like that are called ___ — type the word.

teach it back

🗣️ Now teach it back

Explain to a friend: what a default parameter does (and when it does NOT fire), what a rest parameter gathers, and what makes a function pure — plus why testers prefer pure functions.

Write it as if your friend is sitting next to you. Saved to your journal — future-you will use these notes to teach others.

a few sentences, minimum — you’ve got this
to remember
Default parameter = a plan B penciled into the slot: fires only when the argument would be undefined (null doesn’t count).
Rest parameter (...numbers) = gather ANY number of incoming arguments into one real array. Must be the last parameter.
Pure = same inputs → same output + zero side effects. Everything in via parameters, out via return.
Impure = reaches outside: changes variables, prints, saves. Legal — but every call changes the world.
Tester’s rule: compute pure, side-effect at the edges. expect(add(2,3)).toBe(5) needs no setup — that’s the payoff.