Optional parameters let you make parts of your component or function flexible. You can choose to provide them or not without causing errors.
Optional parameters in Svelte
function greet(name = 'friend') { return `Hello, ${name}!`; } // In Svelte component props: <script> export let color = 'blue'; </script>
In JavaScript functions, you set optional parameters by giving them default values.
In Svelte, you make component props optional by assigning default values when exporting them.
function greet(name = 'friend') { return `Hello, ${name}!`; } console.log(greet()); console.log(greet('Alice'));
<script> export let size = 'medium'; </script> <p>The size is {size}.</p>
<script> export let count = 1; </script> <button>Clicked {count} times</button>
This Svelte component greets a user. If you don't give a name or greeting, it uses 'Guest' and 'Welcome' by default.
<script> export let name = 'Guest'; export let greeting = 'Welcome'; </script> <h1>{greeting}, {name}!</h1>
Always provide default values for optional parameters to avoid undefined errors.
Optional parameters help keep your components flexible and easy to reuse.
Optional parameters let you skip giving some inputs without breaking your code.
In Svelte, use export let with a default value to make props optional.
In JavaScript functions, assign default values to parameters to make them optional.
Practice
Solution
Step 1: Understand Svelte prop declaration
Props are declared withexport letto receive values from outside.Step 2: Assign default value to make prop optional
Assigning a default value like= defaultValuemeans the prop is optional and uses that value if none is passed.Final Answer:
Useexport let propName = defaultValue;-> Option AQuick Check:
Optional prop = export let with default [OK]
- Forgetting to export the prop
- Not assigning a default value
- Using const instead of let for props
Solution
Step 1: Recall JavaScript default parameter syntax
Default values are assigned using= valuein the parameter list.Step 2: Identify correct syntax
function greet(name = 'Guest') { console.log(name); } usesname = 'Guest', which is valid and makes the parameter optional.Final Answer:
function greet(name = 'Guest') { console.log(name); } -> Option DQuick Check:
Default parameter = param = value [OK]
- Using TypeScript syntax like name?
- Using colon instead of equals for default
- Using logical OR inside function body instead of default param
<script>
export let greeting = 'Hello';
export let name;
</script>
<p>{greeting}, {name || 'Friend'}!</p>If used as
<Component /> with no props passed?Solution
Step 1: Check default values of props
greetinghas default 'Hello',nameis undefined because no prop passed.Step 2: Evaluate expression in template
{name || 'Friend'}uses 'Friend' becausenameis falsy (undefined).Final Answer:
Hello, Friend! -> Option AQuick Check:
Default greeting + fallback name = Hello, Friend! [OK]
- Expecting undefined to print as string
- Forgetting default value for greeting
- Assuming name defaults to empty string
<script>
export let count;
if (count === undefined) {
count = 10;
}
</script>Solution
Step 1: Understand Svelte prop reactivity rules
Exported props are reactive and should not be reassigned directly inside the script after declaration.Step 2: Identify correct way to set default
Default values must be assigned during export, likeexport let count = 10;, not by reassigning later.Final Answer:
You cannot assign to an exported prop inside the script after declaration -> Option CQuick Check:
Default props assigned during export only [OK]
- Trying to reassign exported props inside script
- Checking wrong condition for default
- Assuming reassignment updates prop externally
size prop with default 'medium' and an optional color prop with default 'blue'. Which code correctly implements this and uses both props in the template?Solution
Step 1: Check how to declare optional props with defaults
Props must be declared withexport letand assigned default values directly.Step 2: Verify usage in template
Using{size}and{color}inside style attribute is correct.Final Answer:
<script> export let size = 'medium'; export let color = 'blue'; </script> <div style="font-size: {size}; color: {color};">Styled Text</div>-> Option BQuick Check:
export let with default + use in template = correct [OK]
- Not exporting props
- Using const instead of let for props
- Trying to assign defaults after declaration
Ready to prove it?
Pass the test and this pattern moves unknown
