Using else and else-if blocks in Svelte
📖 Scenario: You are building a simple Svelte component that shows a message based on the current hour of the day. This is like a digital greeting card that changes its message depending on the time.
🎯 Goal: Create a Svelte component that uses {#if}, {:else if}, and {:else} blocks to display different greetings for morning, afternoon, evening, and night.
📋 What You'll Learn
Create a variable
hour with a number representing the current hour (0 to 23).Create a variable
morningStart set to 6 and afternoonStart set to 12.Use
{#if}, {:else if}, and {:else} blocks to show:Good morning! if hour is between morningStart and afternoonStart - 1.Good afternoon! if hour is between afternoonStart and 18.Good evening! if hour is between 19 and 21.Good night! for all other hours.💡 Why This Matters
🌍 Real World
Time-based greetings are common in apps and websites to personalize user experience depending on the time of day.
💼 Career
Understanding conditional rendering in Svelte is essential for building interactive user interfaces that respond to data changes.
Progress0 / 4 steps