0
0
Svelteframework~20 mins

Optional parameters in Svelte - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Svelte Optional Parameters Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output when using an optional parameter in a Svelte component?

Consider a Svelte component that accepts a prop with a default value (an optional parameter). What will be rendered if the prop is not passed?

Svelte
<script>
  export let name = "Guest";
</script>

<h1>Hello, {name}!</h1>
A<h1>Hello, undefined!</h1>
B<h1>Hello, Guest!</h1>
C<h1>Hello, !</h1>
DComponent throws an error
Attempts:
2 left
💡 Hint

Think about what default values do when no prop is passed.

📝 Syntax
intermediate
2:00remaining
Which syntax correctly defines an optional parameter with a default value in a Svelte component?

Choose the correct way to define an optional prop count with a default value of 10 in Svelte.

Alet count: number = 10;
Blet count = 10; export count;
Cexport let count = 10;
Dexport count = 10;
Attempts:
2 left
💡 Hint

Remember how Svelte exports props with default values.

state_output
advanced
2:00remaining
What is the output when an optional parameter is updated dynamically?

Given this Svelte component, what will be displayed after clicking the button twice?

Svelte
<script>
  export let start = 5;
  let count = start;
  function increment() {
    count += 1;
  }
</script>

<p>Count: {count}</p>
<button on:click={increment}>Add</button>
ACount: 7
BCount: 5
CCount: 6
DCount: 0
Attempts:
2 left
💡 Hint

Count starts at start and increments by 1 each click.

🔧 Debug
advanced
2:00remaining
Why does this Svelte component fail to use the optional parameter default?

Identify the reason why the default value for title is ignored and undefined is shown.

Svelte
<script>
  export let title;
  if (!title) {
    title = "Default Title";
  }
</script>

<h2>{title}</h2>
ABecause reassigning an exported prop inside the script does not update the reactive value
BBecause the default value must be set in the markup, not script
CBecause Svelte does not allow conditional logic in script
DBecause the prop must be declared with const, not let
Attempts:
2 left
💡 Hint

Think about how Svelte handles exported props and reactivity.

🧠 Conceptual
expert
3:00remaining
How does Svelte handle optional parameters when a parent component passes undefined explicitly?

If a parent component passes undefined explicitly to an optional prop with a default value, what will the child component receive?

AThe child receives <code>null</code> instead of <code>undefined</code>
BThe child uses the default value instead of <code>undefined</code>
CSvelte throws a runtime error for undefined prop
DThe child receives <code>undefined</code>, ignoring the default value
Attempts:
2 left
💡 Hint

Consider how JavaScript treats explicit undefined versus missing props.