📖 Scenario: You want to create a simple webpage that shows a friendly greeting message using Svelte.
🎯 Goal: Build a Svelte component that displays the text "Hello, Svelte!" on the page.
📋 What You'll Learn
Create a Svelte component file
Add a variable to hold the greeting message
Display the greeting message inside the component
Use semantic HTML for accessibility
💡 Why This Matters
🌍 Real World
Creating simple interactive UI components that show dynamic data is a common task in web development.
💼 Career
Knowing how to build basic Svelte components is useful for frontend developer roles that use modern frameworks.
Progress0 / 4 steps
1
Create a greeting variable
Create a variable called greeting and set it to the string "Hello, Svelte!" inside a Svelte component.
Svelte
Hint
Use let greeting = "Hello, Svelte!"; inside the <script> tag.
2
Add a semantic container
Add a <main> element below the <script> tag to hold the greeting message.
Svelte
Hint
Use the <main> tag to wrap your content for better accessibility.
3
Display the greeting message
Inside the <main> element, add the variable {greeting} to display the greeting text.
Svelte
Hint
Use curly braces {} to insert the variable inside the HTML.
4
Add an accessible heading
Inside the <main> element, wrap the greeting variable with an <h1> tag for a clear heading.
Svelte
Hint
Use <h1> to make the greeting a main heading for screen readers and SEO.
Practice
(1/5)
1. What is the main purpose of a Svelte component?
easy
A. To write backend server code
B. To combine HTML, CSS, and JavaScript in one file for UI parts
C. To create database schemas
D. To manage operating system processes
Solution
Step 1: Understand what Svelte components do
Svelte components bundle HTML, CSS, and JavaScript to build user interfaces.
Step 2: Compare options with this purpose
Only To combine HTML, CSS, and JavaScript in one file for UI parts describes combining these parts for UI, others are unrelated.
Final Answer:
To combine HTML, CSS, and JavaScript in one file for UI parts -> Option B
Quick Check:
Svelte components = UI parts combined [OK]
Hint: Remember Svelte bundles UI code in one file [OK]
Common Mistakes:
Thinking Svelte is for backend code
Confusing Svelte with database tools
Assuming Svelte manages OS tasks
2. Which of the following is the correct way to define a simple Svelte component that displays "Hello World!"?
easy
A.
Hello {name}!
B.
Hello World
C. function Hello() { return 'Hello World'; }
D. Hello World
Solution
Step 1: Identify Svelte syntax for components
Svelte uses <script> for logic and HTML with curly braces for variables.
Step 2: Check which option matches this pattern
<script>export let name = 'World';</script><h1>Hello {name}!</h1> uses <script> and {name} interpolation correctly; others are plain HTML or JS functions.
Final Answer:
<script>export let name = 'World';</script><h1>Hello {name}!</h1> -> Option A
Quick Check:
Svelte uses <script> and { } for variables [OK]
Hint: Look for <script> and { } interpolation in Svelte [OK]
Common Mistakes:
Using plain HTML without <script>
Writing JS functions instead of Svelte syntax
Including full HTML document tags inside component
<script>
let message = 'Hi';
</script>
<h1>{message</h1>
medium
A. Script tag should be outside the component
B. Variable message is not declared
C. Missing closing curly brace in {message}
D. Using <h1> tag is invalid in Svelte
Solution
Step 1: Check the template syntax
The expression {message} must have matching braces; here the closing brace is missing.
Step 2: Verify other parts
Variable is declared, <h1> is valid, and <script> is correctly placed.
Final Answer:
Missing closing curly brace in {message} -> Option C
Quick Check:
Curly braces must open and close properly [OK]
Hint: Check all { } pairs in template expressions [OK]
Common Mistakes:
Forgetting to close curly braces
Thinking HTML tags are invalid in Svelte
Misplacing the <script> tag
5. You want to create a Svelte component that shows a greeting only if a user name is provided. Which code correctly implements this?
hard
A.
Hello {user}!
B.
Hello {user || 'Guest'}!
C.
Hello {user}!
D. {#if user}
Hello {user}!
{/if}
Solution
Step 1: Understand conditional rendering in Svelte
Svelte uses {#if condition} ... {/if} blocks to show content conditionally.
Step 2: Check which option uses this correctly
<script>export let user = '';</script>{#if user}<p>Hello {user}!</p>{/if} uses {#if user} to show greeting only if user is not empty; others always show or lack condition.
Final Answer:
<script>export let user = '';</script>{#if user}<p>Hello {user}!</p>{/if} -> Option D
Quick Check:
Use {#if} for conditional display [OK]
Hint: Use {#if} block to show content conditionally [OK]
Common Mistakes:
Not using {#if} for conditional display
Always showing greeting even if user is empty
Using JS ternary inside <p> without Svelte block
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.