By the end of this lesson, you will:
:information_source: What is Svelte Logic? Svelte lets you add logic directly in your HTML! You can use conditional statements and loops without switching to JavaScript. This makes your code cleaner and easier to read.
Here's what makes Svelte logic special:
<script>
tag:memo: Note Unlike JavaScript, Svelte's conditional statements wrap around your HTML code. This keeps your logic and presentation together!
Let's start with the simplest conditional - the if statement:
App.svelte
<script>
import Light from './LightBulb.svelte';
</script>
<Light lightsOn />
LightBulb.svelte
<script>
export let lightsOn;
</script>
\{#if lightsOn\}
<p>The light is turned on! [idea]</p>
\{/if\}
{#if}...{/if}
lightsOn
) acts as the condition{#if condition}...{/if}
:bulb: Tip Think of the if statement like a light switch - when it's true, the content shows up. When it's false, nothing appears!
Now let's add an else statement to handle when our condition is false:
App.svelte
<script>
import Light from './LightBulb.svelte';
</script>
<Light lightsOff />
LightBulb.svelte
<script>
export let lightsOn;
</script>
\{#if lightsOn\}
<p>The light is turned on! [idea]</p>
\{:else\}
<p>It's very dark in here! 🌑</p>
\{/if\}
{:else}
lightsOff
instead of lightsOn
:memo: Note The else statement catches everything that doesn't match your if condition. It's like a safety net!
Sometimes you need more than two options. That's where else if comes in:
App.svelte
<script>
import Light from './LightBulb.svelte';
</script>
<Light lightsBlinking />
LightBulb.svelte
<script>
export let lightsOn;
export let lightsBlinking;
</script>
\{#if lightsOn\}
<p>The light is turned on! [idea]</p>
\{:else if lightsBlinking\}
<p>The light is blinking! [sparkles]. It is time to change the light bulb.</p>
\{:else\}
<p>It's very dark in here! 🌑</p>
\{/if\}
lightsBlinking
as a separate condition{:else if condition}
for additional checks:bulb: Tip Think of else if like a traffic light - you can have multiple states (red, yellow, green), but only one is active at a time!
Let's see how to combine all three conditional statements with value comparisons:
App.svelte
<script>
import Shapes from './Shapes.svelte';
</script>
<Shapes shape=\{"triangle"\} />
Shapes.svelte
<script>
export let shape;
</script>
\{#if shape === 'square'\}
<p>⬜</p>
\{:else if shape === 'triangle'\}
<p>🔺</p>
\{:else if shape === 'circle'\}
<p>🟡</p>
\{:else\}
<p>💙</p>
\{/if\}
===
to check exact valuesSvelte has its own way to loop through arrays:
:information_source: Each Block Instead of JavaScript's forEach() method, Svelte uses the
{#each}...{/each}
block. This special syntax lets you repeat HTML for every item in your array!
App.svelte
<script>
import Name from './studentName.svelte';
</script>
<Name />
studentName.svelte
<script>
let studentNames = ['Chong Wei', 'Yet Phing', 'Naz', 'Jaydon', 'Atta', 'Adibah','Alya'];
</script>
<h1>Telebort Students</h1>
<ul>
\{#each studentNames as goodStudent\}
<li>\{goodStudent\}</li>
\{/each\}
</ul>
{#each arrayName as itemName}...{/each}
goodStudent
represents each student in the loop{itemName}
to show each value:memo: Note You can name the item variable anything you want! Common patterns are:
{#each students as student}
{#each items as item}
{#each names as name}
App.svelte
<script>
let superheroes = [
\{ranking:1, name:"Wolverine"\},
\{ranking:2, name:"Spider-man"\},
\{ranking:3, name:"Captain America"\}
]
</script>
<style>
li \{
padding: 5px;
margin: 5px;
background-color: orange;
color: white;
\}
h1\{
text-align:center;
\}
</style>
<h1>
Superheroes Ranking
</h1>
<ul>
\{#each superheroes as \{ranking, name\}\}
<li>No. \{ranking\} : \{name\} </li>
\{/each\}
</ul>
ranking
and name
){ranking, name}
to extract object properties{#each arrayName as {property1, property2}}...{/each}
:bulb: Tip Destructuring makes your code cleaner! Instead of writing
hero.ranking
andhero.name
, you can useranking
andname
directly.
Let's review what you've learned about Svelte Logic:
{#if condition}...{/if}
- Shows content when true{:else}
- Shows alternative content{:else if condition}
- Adds more conditions{#each array as item}...{/each}
{#each array as {prop1, prop2}}...{/each}
Try these exercises to master Svelte Logic:
Traffic Light Challenge: Create a component that shows different colored circles based on a status
prop (red, yellow, green)
Todo List: Build a simple todo list that loops through an array of tasks and displays them
User Status: Make a component that shows "Online", "Away", or "Offline" based on a user's status prop
Grade Calculator: Create a component that shows letter grades (A, B, C, D, F) based on a numeric score
:memo: Note Practice makes perfect! Try modifying the examples in this lesson to create your own conditional logic and loops.