1. Concept library
  2. JavaScript & TypeScript
  3. Module 2: Functions and overloads

Concept

Optional and default parameters

A ? after a parameter name makes it optional, and = gives it a default. Both let one function serve the common case and the detailed one without overloads.

What a session covers

The tutor works through these with you, in conversation and code. A session ends when you have shown them, not when a video does.

  1. Explain the difference between an optional parameter and one with a default
  2. Write a function that handles a missing argument and one that fills a default
  3. Spot why if (!page) treats 0 as missing, and compare with undefined instead

In practice

A few lines that show Optional and default parameters doing its job.

TypeScript
function paginate(items: string[], size = 10, page?: number) {
  const start = ((page ?? 1) - 1) * size;
  return items.slice(start, start + size);
}

paginate(list);          // first 10
paginate(list, 25);      // first 25
paginate(list, 25, 3);   // third page of 25

The common mistake

Watch for

Checking an optional parameter with if (!page) when 0 or "" is a valid value. The falsy check treats them as missing; compare with undefined or use ?? instead.

What to know first

Your roadmap puts these before Optional and default parameters, unless you already know them.

How it stays learned

Once a session shows you understand Optional and default parameters, it comes back for review. Each review takes a minute and tells the memory model how well you still know it.

  1. Day 3First recall, while it is fresh
  2. Day 7Second, a week on
  3. Day 18Last, when it has settled
Previous in the module← Function types
Next in the moduleOverload signatures →