Recall & Review
beginner
What are optional parameters in Svelte component props?
Optional parameters are props that a Svelte component can receive but are not required. If not provided, they use a default value or remain undefined.
Click to reveal answer
beginner
How do you define a default value for an optional parameter in Svelte?
You assign a default value when declaring the prop using <code>export let propName = defaultValue;</code>. This makes the parameter optional.Click to reveal answer
beginner
What happens if you don't provide an optional parameter when using a Svelte component?
If the optional parameter has a default value, the component uses that value. If no default is set, the parameter is undefined inside the component.
Click to reveal answer
beginner
Show a simple example of a Svelte component with an optional parameter named
color defaulting to 'blue'.<pre><script>
export let color = 'blue';
</script>
<div style="color: {color};">This text is {color}</div></pre>Click to reveal answer
beginner
Why are optional parameters useful in Svelte components?
They let you create flexible components that work with or without certain props, improving reusability and reducing the need to pass every prop every time.Click to reveal answer
How do you make a prop optional in a Svelte component?
✗ Incorrect
In Svelte, giving a prop a default value with
export let prop = value; makes it optional.What is the value of an optional parameter if not provided and no default is set?
✗ Incorrect
If no default value is set and the prop is not passed, its value is
undefined inside the component.Which of these is a correct way to declare an optional parameter
size with default 'medium'?✗ Incorrect
Only
export let size = 'medium'; correctly declares a prop with a default value in Svelte.If a Svelte component has
export let visible = true;, what happens if you use it as <Component /> without passing visible?✗ Incorrect
The default value true is used when the prop is not passed.
Why might you want to use optional parameters in your Svelte components?
✗ Incorrect
Optional parameters let components use default values, making them flexible and easier to reuse.
Explain how to create and use optional parameters in a Svelte component.
Think about how you tell Svelte a prop can have a default value.
You got /3 concepts.
Describe why optional parameters improve component flexibility in Svelte.
Consider how default values help when you don't want to pass every detail every time.
You got /3 concepts.