📖 Scenario: You are building a simple Svelte component for a weather app. The app should show a message about the weather condition. If it is sunny, it should say "It's sunny outside!" Otherwise, it should say "It's not sunny today."
🎯 Goal: Create a Svelte component that uses an {#if} block to display a message based on the weather condition stored in a variable.
📋 What You'll Learn
Create a variable called weather with the value "sunny".
Create a variable called isSunny that is true if weather is exactly "sunny", otherwise false.
Use a Svelte {#if} block to show the text "It's sunny outside!" when isSunny is true.
Use the {:else} block to show the text "It's not sunny today." when isSunny is false.
💡 Why This Matters
🌍 Real World
Conditional rendering is essential in web apps to show or hide parts of the page based on user data or app state, like showing different messages for weather conditions.
💼 Career
Understanding how to use if blocks in frameworks like Svelte is a key skill for frontend developers to build interactive and dynamic user interfaces.
Progress0 / 4 steps
1
Set up the weather variable
Create a variable called weather and set it to the string "sunny".
Svelte
Hint
Use let weather = "sunny"; to create the variable.
2
Create the isSunny variable
Create a variable called isSunny that is true if weather equals "sunny", otherwise false.
Svelte
Hint
Use the strict equality operator === to compare weather with "sunny".
3
Add the if block for sunny weather
Use a Svelte {#if} block to display "It's sunny outside!" only when isSunny is true.
Svelte
Hint
Wrap the message inside {#if isSunny} and {/if} tags.
4
Add the else block for other weather
Add an {:else} block to the existing {#if} block to display "It's not sunny today." when isSunny is false.
Svelte
Hint
Use {:else} followed by the message inside a paragraph tag.
Practice
(1/5)
1. What does the {#if} block do in Svelte?
easy
A. It conditionally shows or hides content based on a true or false value.
B. It loops over a list of items to display them.
C. It styles elements with CSS inside the component.
D. It defines a reusable component.
Solution
Step 1: Understand the purpose of {#if}
The {#if} block is used to check a condition and decide if some content should be shown or hidden.
Step 2: Compare with other options
Looping is done with {#each}, styling is done with CSS, and reusable components are separate files or functions.
Final Answer:
It conditionally shows or hides content based on a true or false value. -> Option A
Quick Check:
Conditional display = C [OK]
Hint: If you want to show/hide, think {#if} block [OK]
Common Mistakes:
Confusing {#if} with loops like {#each}
Thinking {#if} styles elements
Mixing {#if} with component creation
2. Which of the following is the correct syntax for an {#if} block in Svelte?
easy
A. {if condition} ... {endif}
B. <if condition> ... </if>
C. {#if condition} ... {/if}
D. {#if condition} ... {#end}
Solution
Step 1: Recall Svelte if block syntax
Svelte uses {#if condition} ... {/if} to start and end an if block.
Step 2: Check other options for correctness
Options A and C use incorrect syntax from other languages or HTML. {#if condition} ... {#end} uses a wrong closing tag.
Final Answer:
{#if condition} ... {/if} -> Option C
Quick Check:
Correct Svelte if syntax = B [OK]
Hint: Remember Svelte uses {/if} to close if blocks [OK]
Common Mistakes:
Using HTML <if> tags instead of Svelte syntax
Forgetting the # in {#if}
Using wrong closing tags like {#end}
3. What will be rendered by this Svelte code?
{`\n{#if loggedIn}\n
Welcome back!
\n{:else}\n
Please log in.
\n{/if}`}
medium
A.
Welcome back!
B.
Please log in.
C. Nothing will be shown
D. Syntax error
Solution
Step 1: Check the value of loggedIn
The variable loggedIn is set to false initially.
Step 2: Understand the if-else block behavior
If loggedIn is true, it shows "Welcome back!"; else it shows "Please log in." Since loggedIn is false, the else block runs.
Final Answer:
<p>Please log in.</p> -> Option B
Quick Check:
loggedIn false shows else block = A [OK]
Hint: False condition shows {:else} content [OK]
Common Mistakes:
Assuming false shows the if block content
Ignoring the {:else} block
Thinking no output if condition false
4. Identify the error in this Svelte code snippet:
{`\n{#if count > 3}\n
Count is high
\n{:else if count > 1}\n
Count is medium
\n{:else}\n
Count is low
\n{/if}`}
medium
A. The syntax {:else if} is invalid in Svelte.
B. The variable count should be declared with const, not let.