Bird
Raised Fist0
Svelteframework~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What does export let do inside a Svelte component's <script> block?
easy
A. It creates a local variable that cannot be changed.
B. It imports a module from another file.
C. It declares a prop that can be set from outside the component.
D. It defines a function inside the component.

Solution

  1. Step 1: Understand the role of export let

    In Svelte, export let declares a property (prop) that the parent component can pass in.
  2. Step 2: Differentiate from other uses

    Unlike local variables or imports, export let specifically exposes variables as props for customization.
  3. Final Answer:

    It declares a prop that can be set from outside the component. -> Option C
  4. Quick Check:

    Props = export let [OK]
Hint: Remember: export let means external prop [OK]
Common Mistakes:
  • Confusing export let with import statements
  • Thinking export let creates constants
  • Assuming export let defines functions
2. Which of the following is the correct syntax to declare a prop named title with a default value of "Hello" in Svelte?
easy
A. export let title = "Hello";
B. let export title = "Hello";
C. export const title = "Hello";
D. let title = export "Hello";

Solution

  1. Step 1: Recall correct syntax for props

    In Svelte, props are declared with export let followed by the variable name and optional default value.
  2. Step 2: Check each option

    export let title = "Hello"; matches the correct syntax: export let title = "Hello";. Others have wrong order or keywords.
  3. Final Answer:

    export let title = "Hello"; -> Option A
  4. Quick Check:

    export let + name + = default [OK]
Hint: Use 'export let' before variable name for props [OK]
Common Mistakes:
  • Swapping 'let' and 'export' keywords
  • Using 'const' instead of 'let' for props
  • Placing export after variable name
3. Given this Svelte component code:
<script>
  export let count = 5;
</script>

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

What will be displayed if the component is used as <MyComponent count={10} />?
medium
A. Count is 5
B. Count is 10
C. Count is undefined
D. Syntax error

Solution

  1. Step 1: Understand default and passed props

    The component sets a default count of 5, but the parent passes 10, which overrides the default.
  2. Step 2: Determine rendered output

    The displayed text uses the passed value 10, so it shows "Count is 10".
  3. Final Answer:

    Count is 10 -> Option B
  4. Quick Check:

    Passed prop overrides default [OK]
Hint: Passed props override defaults [OK]
Common Mistakes:
  • Assuming default always shows ignoring passed value
  • Thinking undefined appears if prop is passed
  • Confusing syntax error with valid code
4. Identify the error in this Svelte component code:
<script>
  export let name;
  name = "John";
</script>

<p>Hello {name}</p>
medium
A. You cannot assign a value to a prop inside the component after declaration.
B. Missing default value for the prop name.
C. The export keyword should be after let.
D. The <p> tag cannot use curly braces.

Solution

  1. Step 1: Understand prop assignment rules

    Props declared with export let should not be reassigned inside the component; they come from outside.
  2. Step 2: Check the code

    The code tries to assign name = "John" after declaration, which is not allowed and causes an error.
  3. Final Answer:

    You cannot assign a value to a prop inside the component after declaration. -> Option A
  4. Quick Check:

    Props are read-only inside component [OK]
Hint: Don't reassign props inside component [OK]
Common Mistakes:
  • Trying to set default by assignment after export let
  • Confusing export let order
  • Misunderstanding template syntax
5. You want to create a Svelte component that accepts a prop items which is an array, but if no prop is passed, it should default to an empty array. Which code correctly declares this prop?
hard
A. export const items = [];
B. export let items;
if (!items) items = [];
C. let items = [];
D. export let items = [];

Solution

  1. Step 1: Set default value in prop declaration

    In Svelte, you can assign a default value directly when declaring a prop with export let.
  2. Step 2: Evaluate options

    export let items = []; correctly declares items with default empty array. export let items;
    if (!items) items = []; tries to assign after declaration, which is not allowed. let items = []; is local variable, not prop. export const items = []; uses const which is incorrect for props.
  3. Final Answer:

    export let items = []; -> Option D
  4. Quick Check:

    Default values go in export let declaration [OK]
Hint: Assign default value in export let line [OK]
Common Mistakes:
  • Assigning default after declaration
  • Using const instead of let for props
  • Not exporting the variable
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard