Bird
Raised Fist0
Svelteframework~20 mins

Dynamic route parameters in Svelte - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Dynamic Route Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
What is the rendered output for this dynamic route?

Given a SvelteKit route file named src/routes/blog/[slug].svelte with the following code, what will be displayed when navigating to /blog/hello-world?

Svelte
<script>
  export let params;
</script>

<h1>Blog Post: {params.slug}</h1>
A<h1>Blog Post: </h1>
B<h1>Blog Post: [slug]</h1>
C<h1>Blog Post: params.slug</h1>
D<h1>Blog Post: hello-world</h1>
Attempts:
2 left
💡 Hint

Dynamic route parameters are passed as params to the component.

📝 Syntax
intermediate
2:00remaining
Which code correctly accesses a dynamic parameter in a SvelteKit page?

Choose the option that correctly accesses the dynamic route parameter id in a file named src/routes/user/[id].svelte.

A<script>let userId = $page.params.id;</script>
B<script>export let params; let userId = params.id;</script>
C<script>import { page } from '$app/stores'; let userId = page.params.id;</script>
D<script>export let id; let userId = id;</script>
Attempts:
2 left
💡 Hint

Dynamic parameters are passed as params props to the page component.

❓ state_output
advanced
2:00remaining
What is the value of currentId after navigation?

Consider this SvelteKit component in src/routes/product/[id].svelte:

<script>
  export let params;
  let currentId = params.id;
</script>

<h2>Product ID: {currentId}</h2>

If the user navigates from /product/123 to /product/456 without a full page reload, what will be the value of currentId?

A123
B456
Cundefined
DAn error is thrown
Attempts:
2 left
💡 Hint

Think about how export let params works with client-side navigation.

🔧 Debug
advanced
2:00remaining
Why does this dynamic route component show an empty slug?

Given this SvelteKit component src/routes/[slug].svelte:

<script>
  export let params;
</script>

<p>Slug: {params.slug}</p>

When navigating to /hello, the page shows Slug: (empty). What is the most likely cause?

AThe <code>params</code> prop is missing the <code>export let</code> declaration, so it is undefined
BThe <code>params</code> prop is not passed automatically; you must import it from <code>$page</code> store
CThe route parameter name in the filename does not match the accessed property; it should be <code>params.hello</code>
DThe route file is named incorrectly; it should be <code>[slug].js</code> instead of <code>[slug].svelte</code>
Attempts:
2 left
💡 Hint

Check how props are declared in Svelte components.

🧠 Conceptual
expert
3:00remaining
How does SvelteKit handle multiple dynamic parameters in nested routes?

Consider these two route files in SvelteKit:

  • src/routes/shop/[category]/[item].svelte
  • src/routes/shop/[category]/index.svelte

If a user navigates to /shop/electronics/phone, which parameters are available in [item].svelte and what are their values?

AOnly <code>params.item</code> is available with value <code>phone</code>; <code>params.category</code> is undefined
BOnly <code>params.category</code> is available with value <code>electronics</code>; <code>params.item</code> is undefined
CBoth <code>params.category</code> and <code>params.item</code> are available with values <code>electronics</code> and <code>phone</code> respectively
DNo <code>params</code> are available because nested dynamic routes do not pass parameters
Attempts:
2 left
💡 Hint

Think about how nested dynamic segments combine their parameters.

Practice

(1/5)
1. What does a filename like [id].svelte represent in a SvelteKit project?
easy
A. A dynamic route where id is a variable part of the URL
B. A static page named literally 'id'
C. A component that cannot be accessed via URL
D. A special file for global styles

Solution

  1. Step 1: Understand filename brackets in SvelteKit

    Square brackets in filenames define dynamic segments in routes, meaning the part inside brackets is a variable.
  2. Step 2: Interpret [id].svelte

    This means the page matches URLs like /123 or /abc, where id changes dynamically.
  3. Final Answer:

    A dynamic route where id is a variable part of the URL -> Option A
  4. Quick Check:

    Filename with brackets = dynamic route [OK]
Hint: Brackets in filename mean dynamic URL part [OK]
Common Mistakes:
  • Thinking bracket means static filename
  • Confusing dynamic route with component name
  • Assuming it is for styles or scripts
2. Which is the correct way to access a dynamic route parameter named slug inside a SvelteKit page component?
easy
A. Using export let params; in the script
B. Using window.slug in the script
C. Using const { slug } = params; inside load function
D. Using this.slug in the component

Solution

  1. Step 1: Recall how SvelteKit passes route params

    Dynamic route parameters are passed to the page component as a params prop object.
  2. Step 2: Declare the prop in the script

    Inside the page component (+page.svelte) script, use export let params; to receive it, then access params.slug.
  3. Final Answer:

    Using export let params; in the script -> Option A
  4. Quick Check:

    export let params; receives route params [OK]
Hint: export let params; in page script [OK]
Common Mistakes:
  • Using export let slug; (wrong prop name)
  • Accessing params from window or this
  • Confusing with load function params
3. Given a file named src/routes/blog/[postId].svelte with this code:
  <script>
    export let params;
  </script>

  <h1>Post ID: {params.postId}</h1>

What will be rendered when visiting /blog/42?
medium
A.

Post ID: postId

B.

Post ID: 42

C.

Post ID: {params.postId}

D. Error: params is undefined

Solution

  1. Step 1: Understand dynamic route parameter passing

    The [postId].svelte file captures the URL segment after /blog/ as postId and passes it as params.postId.
  2. Step 2: Check how params is used in the component

    The component uses {params.postId} inside the heading, so it will display the actual value from the URL.
  3. Final Answer:

    <h1>Post ID: 42</h1> -> Option B
  4. Quick Check:

    Dynamic param value shown = 42 [OK]
Hint: Dynamic param in URL shows inside {params.paramName} [OK]
Common Mistakes:
  • Expecting literal string 'postId' instead of value
  • Forgetting to export params
  • Using params without export or load
4. You have a dynamic route file [user].svelte but when you visit /john, you get an error: params is undefined. What is the likely cause?
medium
A. You must use window.params to access parameters
B. You named the file incorrectly; it should be {user}.svelte
C. You forgot to export let params in the script
D. Dynamic routes do not support parameters

Solution

  1. Step 1: Check how params are accessed in SvelteKit

    Dynamic route parameters are passed as a prop named params which must be exported in the script.
  2. Step 2: Identify missing export

    If let params is not exported, the component cannot access it, causing params is undefined.
  3. Final Answer:

    You forgot to export let params in the script -> Option C
  4. Quick Check:

    Export params to access dynamic route data [OK]
Hint: Always export let params to use route parameters [OK]
Common Mistakes:
  • Using curly braces in filename instead of brackets
  • Trying to access params from window object
  • Assuming dynamic routes don't pass params
5. You want to create a nested dynamic route in SvelteKit to show a user's post detail at URL /user/123/post/456. Which file structure and parameter access is correct?
hard
A. Files: src/routes/user/[userId]/post.svelte
Access: params.userId only
B. Files: src/routes/user/[userId]/post/[postId].svelte
Access: params.user and params.post
C. Files: src/routes/user/post/[postId].svelte
Access: params.userId and params.postId
D. Files: src/routes/user/[userId]/post/[postId].svelte
Access: params.userId and params.postId

Solution

  1. Step 1: Understand nested dynamic routes in SvelteKit

    To match URL segments like /user/123/post/456, you create nested folders with dynamic filenames for each variable part.
  2. Step 2: Confirm file structure and parameter names

    The correct structure is user/[userId]/post/[postId].svelte, and inside the component, you access params.userId and params.postId.
  3. Final Answer:

    Files: src/routes/user/[userId]/post/[postId].svelte and access params.userId and params.postId -> Option D
  4. Quick Check:

    Nested dynamic folders match URL segments [OK]
Hint: Use nested folders with brackets for each dynamic segment [OK]
Common Mistakes:
  • Using wrong param names inside params object
  • Missing nested folder for second dynamic segment
  • Trying to access params not defined by filename
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard