📖 Scenario: You are building a simple Svelte component to display a user's profile card. Sometimes the user might not provide all the information, so you want to set default values for some props.
🎯 Goal: Create a Svelte component called UserProfile.svelte that accepts props for name, age, and location. Set default values for age and location so the component still shows meaningful info if those props are not passed.
📋 What You'll Learn
Create a name prop without a default value
Create age and location props with default values
Render the props inside the component
Use Svelte syntax for default prop values
💡 Why This Matters
🌍 Real World
Setting default prop values is common in UI components to ensure they display useful information even when some data is missing.
💼 Career
Knowing how to handle default props is essential for frontend developers working with Svelte or similar frameworks to build robust, reusable components.
Progress0 / 4 steps
1
Create the name prop
In UserProfile.svelte, create a name prop using export let name; without assigning a default value.
Svelte
Hint
Use export let name; to declare a prop without a default value.
2
Add default values for age and location props
Add two more props in the <script> block: export let age = 30; and export let location = 'Unknown'; to set default values.
Svelte
Hint
Assign default values directly when declaring the props.
3
Render the props in the component
Below the <script> block, add HTML to display the name, age, and location props inside <p> tags.
Svelte
Hint
Use curly braces {} to insert prop values inside HTML.
4
Complete the component with default prop values
Ensure the full component code includes the <script> block with all three props and the HTML rendering the props as paragraphs.
Svelte
Hint
Check that all props and HTML are present exactly as specified.
Practice
(1/5)
1. What is the correct way to set a default value for a prop named color in a Svelte component?
easy
A. export let color = 'blue';
B. let color = 'blue';
C. prop color = 'blue';
D. export color = 'blue';
Solution
Step 1: Understand Svelte prop declaration
In Svelte, props are declared with export let to make them accessible from outside.
Step 2: Assign default value
Assigning = 'blue' after export let color sets the default value if no prop is passed.
Final Answer:
export let color = 'blue'; -> Option A
Quick Check:
Default prop syntax = export let prop = value [OK]
Hint: Use 'export let' with '=' to set default prop values [OK]
Common Mistakes:
Forgetting 'export' keyword
Using 'let' without export
Using 'prop' keyword which doesn't exist
Assigning default without 'let'
2. Which of the following is the correct syntax to declare a prop size with a default value of 10 in Svelte?
easy
A. export let size: 10;
B. export size = 10;
C. let export size = 10;
D. export let size = 10;
Solution
Step 1: Recognize correct export syntax
The correct way to declare a prop with default is export let prop = value;.
Step 2: Identify correct option
export let size = 10; matches this syntax exactly with size and default 10.
Final Answer:
export let size = 10; -> Option D
Quick Check:
Default prop syntax uses '=' not ':' or misplaced keywords [OK]
<script>
export let message = 'Hello';
</script>
<p>{message}</p>
What will be displayed if the component is used without passing any message prop?
medium
A. An empty paragraph
B. Hello
C. Undefined
D. Error: message not defined
Solution
Step 1: Check default prop value
The prop message has a default value of 'Hello'.
Step 2: Understand usage without prop
If no prop is passed, Svelte uses the default value automatically.
Final Answer:
Hello -> Option B
Quick Check:
Default prop used when no value passed = Hello [OK]
Hint: No prop passed? Default value shows [OK]
Common Mistakes:
Assuming empty string instead of default
Expecting undefined or error
Confusing default with required prop
4. Identify the error in this Svelte component code:
<script>
export let count;
if (count === undefined) {
count = 5;
}
</script>
<p>Count is {count}</p>
medium
A. The count prop must be a string, not a number.
B. The if condition should check for null, not undefined.
C. You cannot assign to a prop after export; default should be set in declaration.
D. The count variable should not be exported.
Solution
Step 1: Understand prop default assignment rules
In Svelte, default prop values must be set during declaration, not by reassigning later.
Step 2: Identify why reassignment is wrong
Reassigning an exported prop inside script causes runtime errors or unexpected behavior.
Final Answer:
You cannot assign to a prop after export; default should be set in declaration. -> Option C
Quick Check:
Default props set at declaration, not by reassignment [OK]
Hint: Set default with export let, not by reassigning later [OK]
Common Mistakes:
Trying to assign default after export
Checking wrong condition for default
Confusing prop type requirements
Not exporting the prop
5. You have a Svelte component that accepts a prop items which should default to an empty array if not provided. Which is the best way to declare this prop to avoid errors when iterating over items in the template?
hard
A. export let items = [];
B. export let items; if (!items) items = [];
C. let items = []; export { items };
D. export let items = null;
Solution
Step 1: Understand default prop for arrays
Setting export let items = []; ensures items is always an array, avoiding errors in iteration.
Step 2: Evaluate other options
export let items; if (!items) items = []; tries to assign after export which is incorrect. let items = []; export { items }; exports a local variable incorrectly. export let items = null; sets null which can cause errors when iterating.
Final Answer:
export let items = []; -> Option A
Quick Check:
Default array props set with export let prop = [] [OK]
Hint: Default arrays with export let prop = [] to avoid iteration errors [OK]
Common Mistakes:
Assigning default after export
Exporting local variables incorrectly
Using null as default for arrays
Not setting default causing runtime errors
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.