📖 Scenario: You are building a simple webpage that shows a list of fruits you like. You want to display each fruit as a separate item on the page.
🎯 Goal: Create a Svelte component that uses the {#each} block to display a list of fruits as an unordered list.
📋 What You'll Learn
Create an array called fruits with the exact values: "Apple", "Banana", "Cherry"
Create a variable called title with the value "My Favorite Fruits"
Use a {#each} block to loop over fruits and display each fruit inside an <li> element
Add a <h2> element that shows the title above the list
💡 Why This Matters
🌍 Real World
Displaying lists of items like products, tasks, or messages is very common in web apps. Using {#each} blocks helps show these lists easily and dynamically.
💼 Career
Knowing how to use {#each} blocks in Svelte is essential for frontend developers working with Svelte to build interactive user interfaces.
Progress0 / 4 steps
1
Create the fruits array
Create an array called fruits with these exact string values: "Apple", "Banana", and "Cherry".
Svelte
Hint
Use let fruits = ["Apple", "Banana", "Cherry"]; inside the <script> tag.
2
Add a title variable
Add a variable called title with the exact value "My Favorite Fruits" inside the <script> tag below the fruits array.
Svelte
Hint
Declare let title = "My Favorite Fruits"; inside the <script> tag.
3
Use {#each} block to list fruits
Below the <script> tag, add an unordered list <ul>. Inside it, use a {#each} block with fruit as the item variable to loop over fruits. For each fruit, create a list item <li> that displays the fruit name.
Svelte
Hint
Use {#each fruits as fruit} to loop and create <li>{fruit}</li> inside the <ul>.
4
Add the title heading
Above the unordered list <ul>, add a heading <h2> that displays the title variable.
Svelte
Hint
Insert <h2>{title}</h2> above the <ul> block.
Practice
(1/5)
1. What does the {#each} block do in Svelte?
easy
A. Creates a conditional statement to show or hide content
B. Repeats a block of HTML for each item in a list
C. Defines a reusable component
D. Declares a reactive variable
Solution
Step 1: Understand the purpose of {#each}
The {#each} block is used to repeat content for every item in a list or array.
Step 2: Compare with other options
Options A, C, and D describe other Svelte features, not list repetition.
Final Answer:
Repeats a block of HTML for each item in a list -> Option B
Quick Check:
{#each} = repeat list items [OK]
Hint: Remember: {#each} loops over lists [OK]
Common Mistakes:
Confusing {#each} with {#if}
Thinking it creates components
Assuming it declares variables
2. Which of the following is the correct syntax to loop over an array items and display each item in Svelte?
easy
A. {#each items to item} <p>{item}</p> {/each}
B. {#each item in items} <p>{item}</p> {/each}
C. {#each items for item} <p>{item}</p> {/each}
D. {#each items as item} <p>{item}</p> {/each}
Solution
Step 1: Recall Svelte each block syntax
The correct syntax is {#each array as item} to loop over an array.
Step 2: Check each option
Only {#each items as item} <p>{item}</p> {/each} uses as correctly. Options A, B, and C use incorrect keywords.
Final Answer:
{#each items as item} <p>{item}</p> {/each} -> Option D
Quick Check:
Use as keyword in {#each} [OK]
Hint: Use 'as' keyword: {#each array as item} [OK]
Common Mistakes:
Using 'in' instead of 'as'
Using 'for' or 'to' keywords
Missing the 'as' keyword
3. Given the code:
{`
{#each fruits as fruit, i}
{i}: {fruit}
{/each}
`} What will be the rendered output inside the <ul>?
medium
A.
0: apple
1: banana
2: cherry
B.
apple: 0
banana: 1
cherry: 2
C.
1: apple
2: banana
3: cherry
D.
fruit: apple
fruit: banana
fruit: cherry
Solution
Step 1: Understand the each block variables
The loop uses fruit for the item and i for the index starting at 0.
Step 2: Match output format
Each list item shows index, colon, then fruit name, so output is 0: apple, 1: banana, 2: cherry.
Final Answer:
<li>0: apple</li><li>1: banana</li><li>2: cherry</li> -> Option A
Quick Check:
Index starts at 0, item after colon [OK]
Hint: Index starts at 0 in {#each items as item, i} [OK]
Common Mistakes:
Confusing index and item order
Starting index at 1 instead of 0
Using wrong variable names
4. What is wrong with this Svelte code snippet?
{`
{#each numbers as number}
{num}
{/each}
`}
medium
A. The variable inside the block should be 'number', not 'num'
B. The array 'numbers' is not defined
C. The each block syntax is incorrect
D. Missing closing {/each} tag
Solution
Step 1: Check variable names inside each block
The loop defines number as the item, but inside the block it uses {num}, which is undefined.
Step 2: Verify other syntax parts
The array is defined, the each block syntax and closing tag are correct.
Final Answer:
The variable inside the block should be 'number', not 'num' -> Option A
Quick Check:
Use same variable name inside each block [OK]
Hint: Use the exact loop variable name inside the block [OK]
Common Mistakes:
Using a different variable name inside the block
Forgetting to close the each block
Misnaming the array variable
5. You want to render a list of users with unique IDs and update the list efficiently when users change. Which is the best way to use {#each} with keys in Svelte?
hard
A. {#each users as user (user.name)} <p>{user.name}</p> {/each}
B. {#each users as user} <p>{user.name}</p> {/each}
C. {#each users as user (user.id)} <p>{user.name}</p> {/each}
D. {#each users as user (index)} <p>{user.name}</p> {/each}
Solution
Step 1: Understand keys in each blocks
Keys help Svelte track items uniquely for efficient updates. The key should be a unique, stable identifier like user.id.
Step 2: Evaluate options
{#each users as user (user.id)} <p>{user.name}</p> {/each} uses user.id as key, which is best. {#each users as user} <p>{user.name}</p> {/each} has no key, less efficient. {#each users as user (user.name)} <p>{user.name}</p> {/each} uses user.name, which may not be unique. {#each users as user (index)} <p>{user.name}</p> {/each} uses index, which can cause issues when list changes.
Final Answer:
{#each users as user (user.id)} <p>{user.name}</p> {/each} -> Option C
Quick Check:
Use unique ID as key in each block [OK]
Hint: Use unique IDs as keys: {#each items as item (item.id)} [OK]
Common Mistakes:
Not using keys at all
Using non-unique keys like names
Using index as key causing update bugs
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.