All topics
Functionsbeginner

Default Parameters

How to give function parameters fallback values that are used automatically when no argument or undefined is passed.

A default is used when an argument is missing or explicitly undefined.

Default parameters are like a restaurant order form that already has 'medium spice' pre-checked — if you don't specify a spice level, you get medium automatically, but if you explicitly circle 'no spice' (an intentional falsy-ish choice), the kitchen respects that instead of silently defaulting you back to medium.

Key Concepts

1
    function greet(name = 'there') {
      return `Hello, ${name}`;
    }
Hello, ${name}
2
    greet();            // 'Hello, there'
    greet(undefined);   // 'Hello, there'
    greet(null);        // 'Hello, null'  <- null is a value
    greet('');          // 'Hello, '      <- so is empty string
3
Only undefined triggers it. That is the difference from the old idiom:
undefined
4
    function old(name) {
      name = name || 'there';     // replaces '' and 0 too
    }
5
Which is the same trap as || versus ?? everywhere else, and it is why defaults are better than the pattern they replaced.
||??
6
Defaults are evaluated at call time, not once at definition:
7
    function log(msg, time = Date.now()) { }
8
Each call gets the current time. Compare with Python, where a default is evaluated once and a mutable default is shared between calls — a classic bug that JavaScript does not have:
9
    function add(item, list = []) { list.push(item); return list; }
    add(1);   // [1]
    add(2);   // [2]  -- a fresh array each time
10
Later parameters can use earlier ones.
    function rect(w, h = w) { }          // a square by default
    function f(a, b = a * 2, c = a + b) { }
11
Only left to right. Referring forwards throws, because the parameter is in its own temporal dead zone.
12
Destructuring with defaults is the pattern used most.
    function setup({ retries = 3, timeout = 1000 } = {}) { }
13
    setup();                    // both defaults
    setup({ retries: 5 });      // timeout still defaults
14
The = {} on the outside is what makes calling with no argument work. Without it, destructuring undefined throws — which is the most common mistake with options objects.
= {}undefined
15
A default marks a parameter optional, and length reflects that:
length
16
    function f(a, b = 1, c) {}
    f.length;   // 1 -- counts up to the first default
17
Rarely important, and occasionally surprising when a library inspects arity.
18
What the interviewer is probing.1. "When does a default actually apply?" Probing: the trigger. Stalls: "For falsy values." Moves up: only for undefined — null, 0 and empty string are passed through, unlike the old || idiom.
19
2. "Can a default reference an earlier parameter?" Probing: evaluation order. Stalls: "No." Moves up: yes — parameters are evaluated left to right, so a later default can use an earlier one.
20
3. "When is a default expression evaluated?" Probing: the timing. Stalls: "Once, at definition." Moves up: on every call where the argument is undefined, which is why a default of [] does not share one array between calls.
21
4. "How do defaults affect function.length?" Probing: the detail. Stalls: "No effect." Moves up: length counts parameters before the first default, which can surprise code that inspects arity.