Using Each Blocks ({#each}) in Svelte
📖 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> elementAdd 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