Bird
Raised Fist0
Svelteframework~20 mins

Keyed each blocks in Svelte - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Keyed Each Blocks Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
What is the output when using a keyed each block with dynamic list updates?

Consider the following Svelte component that uses a keyed each block to render a list of items. What will be the rendered output after clicking the button once?

Svelte
  <script>
    let items = [{ id: 1, name: 'Apple' }, { id: 2, name: 'Banana' }];
    function update() {
      items = [{ id: 2, name: 'Banana' }, { id: 1, name: 'Apple' }];
    }
  </script>

  <button on:click={update}>Swap Items</button>

  <ul>
    {#each items as item (item.id)}
      <li>{item.name}</li>
    {/each}
  </ul>
A<ul><li>Banana</li><li>Banana</li></ul>
B<ul><li>Banana</li><li>Apple</li></ul>
C<ul><li>Apple</li><li>Apple</li></ul>
D<ul><li>Apple</li><li>Banana</li></ul>
Attempts:
2 left
💡 Hint

Remember that keyed each blocks track items by their key and reorder DOM elements instead of recreating them.

❓ state_output
intermediate
2:00remaining
What is the value of the variable after removing an item in a keyed each block?

Given the following Svelte code, what will be the value of items after clicking the 'Remove Banana' button?

Svelte
  <script>
    let items = [{ id: 1, name: 'Apple' }, { id: 2, name: 'Banana' }, { id: 3, name: 'Cherry' }];
    function removeBanana() {
      items = items.filter(item => item.id !== 2);
    }
  </script>

  <button on:click={removeBanana}>Remove Banana</button>

  <ul>
    {#each items as item (item.id)}
      <li>{item.name}</li>
    {/each}
  </ul>
A[{ id: 1, name: 'Apple' }, { id: 3, name: 'Cherry' }]
B[{ id: 2, name: 'Banana' }, { id: 3, name: 'Cherry' }]
C[{ id: 1, name: 'Apple' }, { id: 2, name: 'Banana' }]
D[{ id: 3, name: 'Cherry' }]
Attempts:
2 left
💡 Hint

Filtering removes the item with id === 2.

📝 Syntax
advanced
2:00remaining
Which option correctly uses a keyed each block in Svelte?

Identify the correct syntax for a keyed each block that iterates over users array keyed by user.id.

A{#each users as user, user.id}<p>{user.name}</p>{/each}
B{#each users (user.id) as user}<p>{user.name}</p>{/each}
C{#each users as user key user.id}<p>{user.name}</p>{/each}
D{#each users as user (user.id)}<p>{user.name}</p>{/each}
Attempts:
2 left
💡 Hint

The key expression goes in parentheses immediately after the as clause.

🔧 Debug
advanced
2:00remaining
Why does this keyed each block cause a runtime error?

Examine the code below. Why does it cause a runtime error when rendering?

Svelte
  <script>
    let items = [{ id: 1, name: 'A' }, { id: 2, name: 'B' }];
  </script>

  <ul>
    {#each items as item (item.key)}
      <li>{item.name}</li>
    {/each}
  </ul>
ABecause the key expression must be a string literal, not a property.
BBecause the items array is empty, so nothing renders.
CBecause 'item.key' is undefined; the key must be a valid property on each item.
DBecause the each block is missing a closing {/each} tag.
Attempts:
2 left
💡 Hint

Check the property used as the key in the each block.

🧠 Conceptual
expert
3:00remaining
How does Svelte's keyed each block optimize DOM updates compared to unkeyed each blocks?

Choose the best explanation of how Svelte uses keys in each blocks to optimize rendering.

ASvelte uses keys to track each item uniquely, so it can reorder or update existing DOM nodes instead of recreating them, improving performance.
BKeys in each blocks tell Svelte to always recreate all DOM nodes on every update to ensure freshness.
CSvelte ignores keys and always updates the DOM by replacing the entire list.
DKeys are only used for styling and have no effect on DOM updates.
Attempts:
2 left
💡 Hint

Think about how keys help identify items uniquely during list changes.

Practice

(1/5)
1. What is the main purpose of using a keyed each block in Svelte?
easy
A. To prevent the list from rendering
B. To uniquely identify list items for efficient updates
C. To sort the list items automatically
D. To style list items differently

Solution

  1. Step 1: Understand what keyed each blocks do

    Keyed each blocks use a unique key to track each item in a list, helping Svelte update only changed items.
  2. Step 2: Identify the main purpose

    This unique key helps keep the UI consistent and efficient when the list changes dynamically.
  3. Final Answer:

    To uniquely identify list items for efficient updates -> Option B
  4. Quick Check:

    Keyed each blocks track items uniquely = D [OK]
Hint: Keys track items uniquely for smooth updates [OK]
Common Mistakes:
  • Thinking keys are for styling
  • Assuming keys sort items
  • Believing keys stop rendering
2. Which of the following is the correct syntax for a keyed each block in Svelte?
easy
A. {#each items as item (item.id)}
B. {#each items as item}
C. {#each items (item.id)}
D. {#each items item.id as item}

Solution

  1. Step 1: Recall keyed each block syntax

    The correct syntax uses parentheses after the 'as' clause to specify the key: {#each items as item (item.id)}
  2. Step 2: Compare options

    {#each items as item (item.id)} matches the correct syntax exactly, others miss parentheses or order.
  3. Final Answer:

    {#each items as item (item.id)} -> Option A
  4. Quick Check:

    Key goes in parentheses after 'as' = B [OK]
Hint: Key is inside parentheses after 'as' in each block [OK]
Common Mistakes:
  • Omitting parentheses around the key
  • Placing key before 'as'
  • Using wrong order of parameters
3. Given the code:
{#each users as user (user.id)}
  

{user.name}

{/each}

What happens if the users array changes by adding a new user with a unique id?
medium
A. Only the new user is added to the DOM without re-rendering existing users
B. All users are re-rendered from scratch
C. The list will not update until the page reloads
D. An error occurs because keys must be strings

Solution

  1. Step 1: Understand keyed each block behavior on list change

    When a new item with a unique key is added, Svelte updates only that item in the DOM.
  2. Step 2: Analyze the effect of adding a new user

    The existing users remain unchanged; only the new user is inserted efficiently.
  3. Final Answer:

    Only the new user is added to the DOM without re-rendering existing users -> Option A
  4. Quick Check:

    Keyed blocks update only changed items = C [OK]
Hint: New unique keys add items without full re-render [OK]
Common Mistakes:
  • Thinking all items re-render on change
  • Believing list updates require page reload
  • Assuming keys must be strings only
4. Consider this Svelte code snippet:
{#each items as item (item.key)}
  

{item.value}

{/each}

If item.key is not unique for all items, what problem can occur?
medium
A. Svelte will automatically fix duplicate keys
B. The code will throw a syntax error
C. The list will not render at all
D. Svelte may reuse DOM elements incorrectly, causing UI bugs

Solution

  1. Step 1: Understand the role of unique keys

    Keys must be unique to correctly track each item; duplicates confuse Svelte's DOM updates.
  2. Step 2: Identify the effect of duplicate keys

    With duplicate keys, Svelte may wrongly reuse DOM elements, causing wrong data to show or UI glitches.
  3. Final Answer:

    Svelte may reuse DOM elements incorrectly, causing UI bugs -> Option D
  4. Quick Check:

    Duplicate keys cause wrong DOM reuse = A [OK]
Hint: Keys must be unique to avoid UI bugs [OK]
Common Mistakes:
  • Expecting syntax errors from duplicates
  • Thinking list won't render at all
  • Assuming Svelte fixes duplicates automatically
5. You have a list of tasks with possible duplicate names but unique IDs. You want to display them with a keyed each block and allow dynamic reordering. Which key should you use and why?
{#each tasks as task (???)}
  

{task.name}

{/each}
hard
A. Use the index like each tasks as task (index) for simplicity
B. Use task.name because names are easier to read
C. Use task.id because IDs are unique and stable keys
D. No key is needed for dynamic lists

Solution

  1. Step 1: Identify the best key for dynamic lists

    Keys must be unique and stable to track items correctly during reordering.
  2. Step 2: Compare key options

    Task IDs are unique and stable, while names can duplicate and indexes change on reorder.
  3. Final Answer:

    Use task.id because IDs are unique and stable keys -> Option C
  4. Quick Check:

    Unique stable keys like IDs are best for dynamic lists = A [OK]
Hint: Use unique stable IDs as keys for dynamic lists [OK]
Common Mistakes:
  • Using non-unique names as keys
  • Using index keys causing wrong updates
  • Skipping keys on dynamic lists
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