📖 Scenario: You are building a simple Svelte page to display a list of fruits with their colors. This is like making a small fruit catalog on a webpage.
🎯 Goal: Create a Svelte page that holds a list of fruits and their colors, then show them in a neat list on the page.
📋 What You'll Learn
Create a variable called fruits that holds an array of objects with name and color properties.
Create a variable called highlightColor to store a color string.
Use a {#each} block to loop over fruits and display each fruit's name and color.
Highlight the fruit name with the color stored in highlightColor.
💡 Why This Matters
🌍 Real World
Displaying lists of items with dynamic data is common in websites, like product catalogs or contact lists.
💼 Career
Knowing how to use data and loops in Svelte pages is essential for building interactive web apps and user interfaces.
Progress0 / 4 steps
1
Create the fruits data array
Create a variable called fruits that is an array with these exact objects: { name: 'Apple', color: 'Red' }, { name: 'Banana', color: 'Yellow' }, and { name: 'Grape', color: 'Purple' }.
Svelte
Hint
Use let fruits = [ ... ] and include the objects exactly as shown.
2
Add a highlight color variable
Add a variable called highlightColor and set it to the string 'blue'.
Svelte
Hint
Use let highlightColor = 'blue'; to create the variable.
3
Display the fruits using an each block
Use a {#each} block to loop over fruits and display each fruit's name and color inside a <li> element. Use variables fruit for each item.
Svelte
Hint
Use {#each fruits as fruit} and inside it put a <li> with {fruit.name} and {fruit.color}.
4
Highlight fruit names with the highlight color
Modify the fruit name inside the <li> so that it is wrapped in a <span> with a style attribute that sets color to the highlightColor variable.
Svelte
Hint
Use <span style="color: {highlightColor}">{fruit.name}</span> inside the <li>.
Practice
(1/5)
1. In Svelte, how do you display a variable's value inside the HTML of a page?
easy
A. Use parentheses like (variable) inside the HTML.
B. Use curly braces like {variable} inside the HTML.
C. Use angle brackets like <variable> inside the HTML.
D. Use quotes like "variable" inside the HTML.
Solution
Step 1: Understand Svelte's data binding syntax
Svelte uses curly braces {} to insert JavaScript expressions or variables into HTML.
Step 2: Identify correct syntax for variable display
Using {variable} inside HTML shows the variable's value dynamically.
Final Answer:
Use curly braces like {variable} inside the HTML. -> Option B
Quick Check:
Display variable = {variable} [OK]
Hint: Remember: curly braces show variables in HTML [OK]
Common Mistakes:
Using parentheses instead of curly braces
Trying to use angle brackets for variables
Putting variable names inside quotes
2. Which of the following is the correct way to declare a variable in a Svelte page's <script> block?
easy
A. const name 'Svelte';
B. var name = 'Svelte';
C. let name = 'Svelte';
D. name = 'Svelte';
Solution
Step 1: Recall JavaScript variable declaration in Svelte
Svelte uses standard JavaScript inside <script>, so let is the preferred way to declare variables that can change.
Step 2: Check syntax correctness
let name = 'Svelte'; correctly declares a variable with a string value.
Final Answer:
let name = 'Svelte'; -> Option C
Quick Check:
Variable declaration = let [OK]
Hint: Use 'let' for variables inside <script> [OK]
Common Mistakes:
Missing '=' sign in declaration
Using 'var' which is outdated
Not declaring variable with let/const
3. Given this Svelte code snippet, what will be rendered on the page?
<script>
let fruits = ['Apple', 'Banana', 'Cherry'];
</script>
<ul>
{#each fruits as fruit}
<li>{fruit}</li>
{/each}
</ul>
medium
A.
Apple
Banana
Cherry
B.
fruits
C. Apple, Banana, Cherry (plain text, no list)
D. Syntax error, nothing renders
Solution
Step 1: Understand the {#each} block
The {#each} block loops over the array fruits and creates an <li> for each item.
Step 2: Check the output structure
Each fruit name is inserted inside <li>, so the output is a list with three items.
Final Answer:
<ul><li>Apple</li><li>Banana</li><li>Cherry</li></ul> -> Option A
Quick Check:
Loop over fruits = list items [OK]
Hint: {#each} creates list items for array elements [OK]
Common Mistakes:
Expecting plain text instead of list
Misunderstanding {#each} syntax
Thinking it outputs variable name instead of values