Bird
Raised Fist0
Svelteframework~10 mins

Each blocks ({#each}) in Svelte - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Each blocks ({#each})
Start
↓
Receive array
↓
Check if array is empty?
Yes→Render nothing or else block
No↓
Loop over each item
↓
Render item block
↓
Repeat for next item
↓
End loop
↓
Finish rendering
The {#each} block loops over an array and renders a block for each item. If the array is empty, it can render an else block or nothing.
Execution Sample
Svelte
<script>
  let fruits = ['Apple', 'Banana', 'Cherry'];
</script>

<ul>
  {#each fruits as fruit}
    <li>{fruit}</li>
  {/each}
</ul>
This code loops over the fruits array and renders each fruit inside a list item.
Execution Table
StepActionCurrent item (fruit)Rendered output
1Start loop over fruitsN/A<ul>
2Render first itemApple<li>Apple</li>
3Render second itemBanana<li>Banana</li>
4Render third itemCherry<li>Cherry</li>
5End loopN/A</ul>
6Finish renderingN/A<ul><li>Apple</li><li>Banana</li><li>Cherry</li></ul>
💡 All items in fruits array rendered, loop ends.
Variable Tracker
VariableStartAfter 1After 2After 3Final
fruits['Apple', 'Banana', 'Cherry']['Apple', 'Banana', 'Cherry']['Apple', 'Banana', 'Cherry']['Apple', 'Banana', 'Cherry']['Apple', 'Banana', 'Cherry']
fruitundefinedAppleBananaCherryundefined
Key Moments - 3 Insights
Why does the variable 'fruit' change during the loop?
Because the {#each} block assigns 'fruit' to each item in the array one by one, as shown in steps 2-4 in the execution table.
What happens if the array is empty?
The loop does not run and nothing inside the {#each} block renders unless an {#else} block is provided.
Can you use an index inside the {#each} block?
Yes, you can write {#each fruits as fruit, i} to get the index 'i' for each item.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of 'fruit' at step 3?
ACherry
BApple
CBanana
Dundefined
💡 Hint
Check the 'Current item (fruit)' column at step 3 in the execution table.
At which step does the loop finish rendering all items?
AStep 5
BStep 4
CStep 6
DStep 2
💡 Hint
Look for the 'End loop' action in the execution table.
If the fruits array was empty, what would happen in the execution table?
AThe loop would run infinitely
BSteps 2-4 would be skipped
CThe loop would run once with fruit undefined
DThe code would throw an error
💡 Hint
Recall that an empty array means no iterations in the {#each} block.
Concept Snapshot
Svelte {#each} loops over arrays.
Syntax: {#each array as item} ... {/each}
Renders block for each item.
Can use index: {#each array as item, i}
If empty, renders nothing or {#else} block.
Full Transcript
The Svelte {#each} block lets you loop over an array and render HTML for each item. It assigns each item to a variable you choose, like 'fruit' in the example. The loop runs once per item, rendering the block inside. If the array is empty, the block does not render unless you add an {#else} block. You can also get the index of each item by adding a second variable. This helps create lists dynamically in your app.

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

  1. Step 1: Understand the purpose of {#each}

    The {#each} block is used to repeat content for every item in a list or array.
  2. Step 2: Compare with other options

    Options A, C, and D describe other Svelte features, not list repetition.
  3. Final Answer:

    Repeats a block of HTML for each item in a list -> Option B
  4. 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

  1. Step 1: Recall Svelte each block syntax

    The correct syntax is {#each array as item} to loop over an array.
  2. 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.
  3. Final Answer:

    {#each items as item} <p>{item}</p> {/each} -> Option D
  4. 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

    1. Step 1: Understand the each block variables

      The loop uses fruit for the item and i for the index starting at 0.
    2. Step 2: Match output format

      Each list item shows index, colon, then fruit name, so output is 0: apple, 1: banana, 2: cherry.
    3. Final Answer:

      <li>0: apple</li><li>1: banana</li><li>2: cherry</li> -> Option A
    4. 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

    1. 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.
    2. Step 2: Verify other syntax parts

      The array is defined, the each block syntax and closing tag are correct.
    3. Final Answer:

      The variable inside the block should be 'number', not 'num' -> Option A
    4. 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

    1. 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.
    2. 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.
    3. Final Answer:

      {#each users as user (user.id)} <p>{user.name}</p> {/each} -> Option C
    4. 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 unknown strong while earning +1 Grip toward your certificate.

    10 questions~5 mineasy hard