Consider this Svelte component that uses a default prop value:
<script>
export let name = 'Guest';
</script>
<p>Hello, {name}!</p>What will be the output if this component is used without passing any prop?
<script>
export let name = 'Guest';
</script>
<p>Hello, {name}!</p>Think about what happens when no value is passed for a prop with a default.
Since name has a default value of 'Guest', it is used when no prop is passed.
Which of the following code snippets correctly sets a default value for a prop count in a Svelte component?
Remember how to export props with default values in Svelte.
Only option B correctly exports a prop with a default value. Option B has invalid syntax with multiple script tags and does not set a default, B does not export, C has invalid syntax.
Given this Svelte component:
<script>
export let title = 'Default Title';
</script>
<h1>{title}</h1>What will be rendered if the component is used like this?
<Component title={undefined} /><script> export let title = 'Default Title'; </script> <h1>{title}</h1>
Think about how Svelte treats explicitly passed undefined props versus missing props.
Passing undefined explicitly overrides the default value, so title becomes undefined and renders as the string 'undefined'.
Look at this Svelte component:
<script>
export let items = [];
</script>
<ul>
{#each items as item}
<li>{item}</li>
{/each}
</ul>When used without passing items, the list is empty. But when used multiple times, all instances share the same items array. Why?
<script> export let items = []; </script> <ul> {#each items as item} <li>{item}</li> {/each} </ul>
Think about how default values for objects or arrays behave in JavaScript.
Default prop values that are objects or arrays are shared references, so all component instances share the same array, causing unexpected shared state.
When a Svelte component with default prop values is server-rendered and then hydrated on the client, how are the default prop values treated if the client does not pass those props?
Consider how Svelte merges server and client props during hydration.
Svelte uses default prop values on the client during hydration if no props are passed, ensuring consistent behavior between server and client.