Concept Flow - Else and else-if blocks
Start
Check if condition1
Render block1
Render block2
End
Svelte checks conditions in order: if, else-if, then else. It renders the first true block and skips the rest.
<script> let count = 2; </script> {#if count === 1} <p>One</p> {:else if count === 2} <p>Two</p> {:else} <p>Other</p> {/if}
| Step | Condition Checked | Condition Result | Block Rendered |
|---|---|---|---|
| 1 | count === 1 | False | No |
| 2 | count === 2 | True | <p>Two</p> |
| 3 | else block | Skipped | No |
| Variable | Start | After Step 1 | After Step 2 | Final |
|---|---|---|---|---|
| count | 2 | 2 | 2 | 2 |
Svelte uses {#if} for conditions.
Use {:else if} for additional checks.
Use {:else} for fallback.
Only the first true block renders.
Others are skipped.
Syntax example:
{#if cond1} ... {:else if cond2} ... {:else} ... {/if}