01 / 20
= ... args

Default & Rest
Parameters

Master two essential JavaScript features that make your functions flexible, resilient, and ready for any number of arguments.

JavaScript

What Are Parameters?

Every function you write can accept inputs called parameters. They act as placeholders that receive arguments when the function is called. But what happens when an argument is missing or you don't know how many will arrive?

function greet(name) { return "Hello, " + name; } greet("Alice"); // "Hello, Alice" greet(); // "Hello, undefined" ← problem!

Introducing Default Parameters

A default parameter lets you assign a fallback value right in the function signature. If the caller omits that argument (or passes undefined), the default kicks in automatically.

function greet(name = "World") { return "Hello, " + name; } greet("Alice"); // "Hello, Alice" greet(); // "Hello, World" ← default used!

Introducing Rest Parameters

A rest parameter collects all remaining arguments into a real Array. It uses the ... (spread/rest) syntax and must always be the last parameter.

function sum(...numbers) { return numbers.reduce((a, b) => a + b, 0); } sum(1, 2, 3); // 6 sum(10, 20); // 30 sum(); // 0 (empty array)

How Default Values Resolve

function greet(name = "World") parameter with default greet("Alice") → "Hello, Alice" greet() → "Hello, World" Argument passed? YES Use provided value NO / undefined Use default value Note: null does NOT trigger the default, only undefined does

How Rest Collects Arguments

function logAll(first, ...rest) Call: logAll("a", "b", "c", "d") "a" first "b" "c" "d" ...rest = ["b", "c", "d"] first → "a" rest → ["b", "c", "d"] (true Array!)

The Formal Rules

1
Default parameters use = in the signature. They activate only when the argument is undefined (not null, not 0, not "").
2
Defaults are evaluated at call time, not at definition time. Each call gets a fresh copy of the default value.
3
Rest parameters use ... prefix. They collect zero or more remaining arguments into an Array.
4
A function can have at most one rest parameter, and it must be the last parameter in the list.
5
You can combine both: place defaults before the rest parameter for maximum flexibility.

Rest vs arguments Object

...rest arguments
Type True Array Array-like object
Array methods ✓ .map(), .filter(), etc. ✕ Must convert first
Arrow functions ✓ Works everywhere ✕ Not available
Named params ✓ Collects only extras Contains all arguments
Readability Explicit intent Hidden / implicit

How to Apply These Features

1
Identify optional parameters: Which arguments might the caller skip? Give those a sensible default value.
2
Choose the default wisely: Use the most common value, an empty string, zero, or an empty array: whatever keeps the function running correctly.
3
Use rest for variable-length input: When a function might receive 2, 5, or 100 items, use ...args to collect them.
4
Place rest last: Any named parameters go first, then ...rest collects the remainder.
5
Combine them: function create(type = "basic", ...options) is a perfectly valid signature.

Example 1: Default in Action

function createUser(name, role = "viewer", active = true) { return { name, role, active }; } createUser("Alice", "admin"); // { name: "Alice", role: "admin", active: true } createUser("Bob"); // { name: "Bob", role: "viewer", active: true } createUser("Cara", undefined, false); // { name: "Cara", role: "viewer", active: false }

Example 2: Rest in Action

function sendMessage(sender, ...recipients) { recipients.forEach(r => { console.log(`${sender} → ${r}`); }); } sendMessage("Alice", "Bob", "Cara", "Dave"); // Alice → Bob // Alice → Cara // Alice → Dave

Example 3: Combining Both

function buildTag(tag = "div", ...classes) { const el = document.createElement(tag); el.classList.add(...classes); return el; } buildTag("section", "hero", "dark"); // <section class="hero dark"></section> buildTag(undefined, "container"); // <div class="container"></div> ← default "div"

Defaults Can Be Expressions

Default values aren't limited to simple literals. You can use function calls, other parameters, and expressions as defaults. They evaluate left to right at call time.

function makeId(prefix = "id", num = Math.random()) { return `${prefix}_${num}`; } makeId(); // "id_0.7231..." (fresh each call) makeId("user"); // "user_0.4912..." function repeat(str, times = str.length) { return str.repeat(times); } repeat("ha"); // "haha" (times defaults to 2)

Common Mistakes to Avoid

✕ Rest Not Last
Writing function f(...a, b) throws a SyntaxError. The rest parameter must always be the final parameter.
✕ Null vs Undefined
Passing null does NOT trigger a default. Only undefined (or omitting the argument) activates it.
✕ Using arguments
Mixing arguments object with rest params causes confusion. Pick one: prefer ...rest in modern code.

Summary Comparison

Default Parameters Rest Parameters
Syntax param = value ...param
Purpose Fallback for missing args Collect extra args
Position Any position (best at end) Must be last
Result type Single value Array
Trigger undefined / omitted Always active
Count allowed Multiple defaults OK Only one per function

Question 1 of 5

What is the output of the following code?

function greet(name = "World") { return "Hi, " + name; } console.log(greet(null));

Question 2 of 5

Which placement of a rest parameter is valid?

Question 3 of 5

What is the output?

function collect(a, ...rest) { return rest; } console.log(collect(1, 2, 3));

Question 4 of 5

What is the output?

function add(a = 10, b = 20) { return a + b; } console.log(add(5, undefined));

Question 5 of 5

What does typeof rest return inside a function declared as function f(...rest) {}?