0
0
Svelteframework~20 mins

Declaring props with export let in Svelte - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Svelte Props Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What will this Svelte component display?

Consider this Svelte component code:

  <script>
  export let name = 'Guest';
</script>

<h1>Hello {name}!
</h1>

If you use this component without passing any prop, what will be shown on the page?

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

<h1>Hello {name}!</h1>
AHello name!
BHello !
CHello undefined!
DHello Guest!
Attempts:
2 left
💡 Hint

Think about the default value assigned to the prop.

📝 Syntax
intermediate
2:00remaining
Which option correctly declares a prop named count with default 0?

Choose the correct Svelte syntax to declare a prop count with a default value of 0.

Aexport let count = 0;
Blet export count = 0;
Clet count export = 0;
Dexport count let = 0;
Attempts:
2 left
💡 Hint

Remember the order: export then let.

state_output
advanced
2:00remaining
What is the output after updating a prop inside the component?

Given this Svelte component:

<script>
  export let value = 5;
  value = value + 3;
</script>

<p>Value is {value}</p>

What will be displayed when the component is used with <Component value={10} />?

Svelte
<script>
export let value = 5;
value = value + 3;
</script>

<p>Value is {value}</p>
AValue is 13
BValue is 8
CValue is 5
DValue is 10
Attempts:
2 left
💡 Hint

Remember the prop value is overwritten inside the component.

🔧 Debug
advanced
2:00remaining
Why does this Svelte component throw an error?

Look at this code snippet:

<script>
  export let count;
  count = count + 1;
</script>

<p>Count is {count}</p>

When used without passing count, it throws an error. Why?

Svelte
<script>
export let count;
count = count + 1;
</script>

<p>Count is {count}</p>
ABecause the component is missing a return statement
BBecause count is undefined and adding 1 to undefined causes an error
CBecause count must be declared with const, not let
DBecause export let cannot be reassigned inside the component
Attempts:
2 left
💡 Hint

Think about what happens when you add a number to undefined.

🧠 Conceptual
expert
2:00remaining
What happens if you declare a prop without export in Svelte?

Consider this Svelte component snippet:

<script>
  let title = 'Hello';
</script>

<h1>{title}</h1>

If you try to pass a prop named title from a parent component, what will happen?

Svelte
<script>
let title = 'Hello';
</script>

<h1>{title}</h1>
AThe component will receive and display the passed title prop
BSvelte will throw a syntax error for missing export
CThe prop will not be received; the component uses its internal title variable
DThe component will crash at runtime
Attempts:
2 left
💡 Hint

Think about how Svelte knows which variables are props.