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.
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.
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.
⌨️ 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
areawith two parameters:width, andheightwhose default value iswidthitself. - It RETURNS width × height — no printing inside the function (keep it pure!).
- Print
area(3)(a 3×3 square — the default kicks in), thenarea(2, 5).
when you press RUN, the console must show exactly:
✏️ 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.
🗣️ 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.