Discover how a tiny loop can save you hours of tedious work and bugs!
Why Each blocks ({#each}) in Svelte? - Purpose & Use Cases
Imagine you have a list of 10 items and you want to show them all on a webpage. You write HTML for each item by hand, and every time the list changes, you have to update the HTML manually.
Manually updating each item is slow and easy to mess up. If the list grows or changes often, you waste time rewriting code and risk forgetting to update some parts, causing bugs or inconsistent displays.
Svelte's {#each} block lets you write one simple loop that automatically creates HTML for every item in your list. When the list changes, Svelte updates the page for you, saving time and avoiding mistakes.
<ul>\n <li>Item 1</li>\n <li>Item 2</li>\n <li>Item 3</li>\n</ul>
<ul>\n {#each items as item}\n <li>{item}</li>\n {/each}\n</ul>You can easily display and update lists of any size with clean, simple code that stays correct as your data changes.
Think of a shopping list app where users add or remove items. Using {#each}, the app instantly shows the updated list without you rewriting HTML every time.
Manually writing repeated HTML is slow and error-prone.
{#each} automates creating HTML for lists.
This makes your code simpler, cleaner, and easier to maintain.
Practice
{#each} block do in Svelte?Solution
Step 1: Understand the purpose of
The{#each}{#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 BQuick Check:
{#each}= repeat list items [OK]
{#each} loops over lists [OK]- Confusing
{#each}with{#if} - Thinking it creates components
- Assuming it declares variables
items and display each item in Svelte?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}usesascorrectly. Options A, B, and C use incorrect keywords.Final Answer:
{#each items as item} <p>{item}</p> {/each} -> Option DQuick Check:
Useaskeyword in{#each}[OK]
- Using 'in' instead of 'as'
- Using 'for' or 'to' keywords
- Missing the 'as' keyword
{`
-
{#each fruits as fruit, i}
- {i}: {fruit} {/each}
What will be the rendered output inside the
<ul>?Solution
Step 1: Understand the each block variables
The loop usesfruitfor the item andifor the index starting at 0.Step 2: Match output format
Each list item shows index, colon, then fruit name, so output is0: apple,1: banana,2: cherry.Final Answer:
<li>0: apple</li><li>1: banana</li><li>2: cherry</li> -> Option AQuick Check:
Index starts at 0, item after colon [OK]
- Confusing index and item order
- Starting index at 1 instead of 0
- Using wrong variable names
{`
-
{#each numbers as number}
- {num} {/each}
Solution
Step 1: Check variable names inside each block
The loop definesnumberas 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 AQuick Check:
Use same variable name inside each block [OK]
- Using a different variable name inside the block
- Forgetting to close the each block
- Misnaming the array variable
{#each} with keys in Svelte?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 likeuser.id.Step 2: Evaluate options
{#each users as user (user.id)} <p>{user.name}</p> {/each}usesuser.idas 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}usesuser.name, which may not be unique.{#each users as user (index)} <p>{user.name}</p> {/each}usesindex, which can cause issues when list changes.Final Answer:
{#each users as user (user.id)} <p>{user.name}</p> {/each} -> Option CQuick Check:
Use unique ID as key in each block [OK]
- Not using keys at all
- Using non-unique keys like names
- Using index as key causing update bugs
Ready to prove it?
Pass the test and this pattern moves unknown
