0Pricing
Sveltejs Academy · Lesson

Optional and Rest Parameters

Handle optional segments with [[optional]] and catch-all routes with [...rest].

Optional and Rest Parameters is a free Sveltejs Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Sveltejs Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Optional Segments

Use double brackets [[optional]] to make a segment optional.

src/routes/[[lang]]/about/+page.svelte

Both URLs Match

The above matches both /about and /en/about. lang is undefined for the first.

Rest Parameters

Use [...rest] to capture all remaining path segments as an array.

src/routes/docs/[...slug]/+page.svelte

Capturing Many Segments

The above matches /docs, /docs/a, /docs/a/b, etc. params.slug is the joined string.

Use Cases

Rest params suit docs sites, wikis, and file browsers where path depth varies.

Combining

Mix optional and rest with normal params for flexible routing.

Catch-All Last

Rest params must be the last segment; SvelteKit enforces this.

Decoding

Path segments are URL-decoded; encode special chars when building URLs.

Empty Rest

An empty rest param resolves to an empty string. Handle that in your load function.

SEO Friendly

Rest routes can serve nested docs without huge route trees.

Conflict Resolution

SvelteKit prefers specific routes over rest ones. Static beats rest.

Quick Check

What does [...slug] do?

Recap

Use [[opt]] for optional, [...rest] for catch-all. Both expand routing flexibility.

Frequently asked questions

Is the “Optional and Rest Parameters” lesson free?

Yes — the full text of “Optional and Rest Parameters” is free to read here on the web, and the Sveltejs Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Sveltejs Academy course, upgrade to CoddyKit PRO.

What will I learn in “Optional and Rest Parameters”?

Handle optional segments with [[optional]] and catch-all routes with [...rest]. You practise Sveltejs Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Sveltejs Academy?

No prior experience is required. Sveltejs Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Optional and Rest Parameters” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Sveltejs Academy lesson?

Yes. Every Sveltejs Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. File-based Routing with +page.svelte
  2. Dynamic Route Parameters: [id]
  3. Optional and Rest Parameters
  4. Layout Files: +layout.svelte
← Back to Sveltejs Academy