<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.