Bird
Raised Fist0
Svelteframework~20 mins

If blocks ({#if}) in Svelte - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Svelte If Blocks 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 render?
Consider this Svelte component code:
<script>
  let count = 3;
</script>

{#if count > 5}
  <p>Count is greater than 5</p>
{:else if count > 0}
  <p>Count is positive but 5 or less</p>
{:else}
  <p>Count is zero or negative</p>
{/if}

What will be rendered in the browser?
Svelte
<script>
  let count = 3;
</script>

{#if count > 5}
  <p>Count is greater than 5</p>
{:else if count > 0}
  <p>Count is positive but 5 or less</p>
{:else}
  <p>Count is zero or negative</p>
{/if}
ANo output rendered
B<p>Count is zero or negative</p>
C<p>Count is greater than 5</p>
D<p>Count is positive but 5 or less</p>
Attempts:
2 left
💡 Hint
Check the value of count and which condition matches first.
📝 Syntax
intermediate
2:00remaining
Identify the syntax error in this Svelte if block
Which option correctly fixes the syntax error in this Svelte code?
{#if isActive}
  <p>Active</p>
{:else}
  <p>Inactive</p>
{/if
Svelte
{#if isActive}
  <p>Active</p>
{:else}
  <p>Inactive</p>
{/if
ARemove the closing {/if block entirely
BChange {/if to {/if} to close the block properly
CAdd a semicolon after {/if to fix syntax
DReplace {:else} with {:else if} to fix the error
Attempts:
2 left
💡 Hint
Check the closing tag of the if block carefully.
❓ state_output
advanced
2:00remaining
What is the output after clicking the button twice?
Given this Svelte component:
<script>
  let count = 0;
</script>

{#if count % 2 === 0}
  <p>Even count: {count}</p>
{:else}
  <p>Odd count: {count}</p>
{/if}

<button on:click={() => count++}>Increment</button>

What will the paragraph show after clicking the button two times?
Svelte
<script>
  let count = 0;
</script>

{#if count % 2 === 0}
  <p>Even count: {count}</p>
{:else}
  <p>Odd count: {count}</p>
{/if}

<button on:click={() => count++}>Increment</button>
A<p>Even count: 2</p>
B<p>Odd count: 2</p>
C<p>Even count: 1</p>
D<p>Odd count: 1</p>
Attempts:
2 left
💡 Hint
Count how many times the button increments and check parity.
🔧 Debug
advanced
2:00remaining
Why does this Svelte if block not update on variable change?
This component does not update the displayed text when the button is clicked:
<script>
  let visible = false;
  function toggle() {
    visible = !visible;
  }
</script>

{#if visible}
  <p>Visible</p>
{/if}

<button on:click={toggle}>Toggle</button>

What is the most likely reason the paragraph never appears?
Svelte
<script>
  let visible = false;
  function toggle() {
    visible = !visible;
  }
</script>

{#if visible}
  <p>Visible</p>
{/if}

<button on:click={toggle}>Toggle</button>
AThe code is correct; the paragraph should appear when button is clicked
BThe variable 'visible' is not reactive because it is not declared with $: prefix
CThe component does not re-render because 'visible' is not reassigned properly
DThe toggle function does not update 'visible' because it lacks a return statement
Attempts:
2 left
💡 Hint
Svelte tracks changes to let variables automatically.
🧠 Conceptual
expert
3:00remaining
How does Svelte optimize {#if} blocks during compilation?
Which statement best describes how Svelte handles {#if} blocks when compiling components?
ASvelte compiles all possible {#if} block contents into the DOM and toggles their visibility with CSS
BSvelte uses a virtual DOM diffing algorithm to update the UI when {#if} conditions change
CSvelte compiles {#if} blocks into JavaScript that adds or removes DOM nodes dynamically without a virtual DOM
DSvelte requires manual DOM manipulation code inside {#if} blocks to update the UI
Attempts:
2 left
💡 Hint
Think about how Svelte differs from React in updating the UI.

Practice

(1/5)
1. What does the {#if} block do in Svelte?
easy
A. It conditionally shows or hides content based on a true or false value.
B. It loops over a list of items to display them.
C. It styles elements with CSS inside the component.
D. It defines a reusable component.

Solution

  1. Step 1: Understand the purpose of {#if}

    The {#if} block is used to check a condition and decide if some content should be shown or hidden.
  2. Step 2: Compare with other options

    Looping is done with {#each}, styling is done with CSS, and reusable components are separate files or functions.
  3. Final Answer:

    It conditionally shows or hides content based on a true or false value. -> Option A
  4. Quick Check:

    Conditional display = C [OK]
Hint: If you want to show/hide, think {#if} block [OK]
Common Mistakes:
  • Confusing {#if} with loops like {#each}
  • Thinking {#if} styles elements
  • Mixing {#if} with component creation
2. Which of the following is the correct syntax for an {#if} block in Svelte?
easy
A. {if condition} ... {endif}
B. <if condition> ... </if>
C. {#if condition} ... {/if}
D. {#if condition} ... {#end}

Solution

  1. Step 1: Recall Svelte if block syntax

    Svelte uses {#if condition} ... {/if} to start and end an if block.
  2. Step 2: Check other options for correctness

    Options A and C use incorrect syntax from other languages or HTML. {#if condition} ... {#end} uses a wrong closing tag.
  3. Final Answer:

    {#if condition} ... {/if} -> Option C
  4. Quick Check:

    Correct Svelte if syntax = B [OK]
Hint: Remember Svelte uses {/if} to close if blocks [OK]
Common Mistakes:
  • Using HTML <if> tags instead of Svelte syntax
  • Forgetting the # in {#if}
  • Using wrong closing tags like {#end}
3. What will be rendered by this Svelte code?
{`\n{#if loggedIn}\n  

Welcome back!

\n{:else}\n

Please log in.

\n{/if}`}
medium
A.

Welcome back!

B.

Please log in.

C. Nothing will be shown
D. Syntax error

Solution

  1. Step 1: Check the value of loggedIn

    The variable loggedIn is set to false initially.
  2. Step 2: Understand the if-else block behavior

    If loggedIn is true, it shows "Welcome back!"; else it shows "Please log in." Since loggedIn is false, the else block runs.
  3. Final Answer:

    <p>Please log in.</p> -> Option B
  4. Quick Check:

    loggedIn false shows else block = A [OK]
Hint: False condition shows {:else} content [OK]
Common Mistakes:
  • Assuming false shows the if block content
  • Ignoring the {:else} block
  • Thinking no output if condition false
4. Identify the error in this Svelte code snippet:
{`\n{#if count > 3}\n  

Count is high

\n{:else if count > 1}\n

Count is medium

\n{:else}\n

Count is low

\n{/if}`}
medium
A. The syntax {:else if} is invalid in Svelte.
B. The variable count should be declared with const, not let.
C. The closing tag {/if} is missing.
D. There is no error; the code is correct.

Solution

  1. Step 1: Check if {:else if} is valid in Svelte

    Svelte supports {:else if} syntax to chain conditions inside {#if} blocks.
  2. Step 2: Verify variable declaration and closing tags

    Using let for count is correct for reactive variables. The {/if} closing tag is present.
  3. Final Answer:

    There is no error; the code is correct. -> Option D
  4. Quick Check:

    Valid else if syntax and proper closure = A [OK]
Hint: Svelte supports {:else if} inside {#if} blocks [OK]
Common Mistakes:
  • Thinking {:else if} is invalid syntax
  • Forgetting {/if} closing tag
  • Confusing let and const for reactive vars
5. You want to display a message only if a user is logged in and has admin rights. Which Svelte {#if} block correctly checks both conditions?
hard
A. {#if loggedIn && isAdmin}

Welcome, admin!

{/if}
B. {#if loggedIn || isAdmin}

Welcome, admin!

{/if}
C. {#if loggedIn} {:if isAdmin}

Welcome, admin!

{/if} {/if}
D. {#if loggedIn | isAdmin}

Welcome, admin!

{/if}

Solution

  1. Step 1: Understand the condition requirements

    The message should show only if both loggedIn and isAdmin are true, so we need a logical AND.
  2. Step 2: Evaluate each option's logic

    {#if loggedIn | isAdmin}

    Welcome, admin!

    {/if}
    uses | bitwise OR which shows if either is true (wrong). {#if loggedIn || isAdmin}

    Welcome, admin!

    {/if}
    uses || logical OR which means either can be true (wrong). {#if loggedIn} {:if isAdmin}

    Welcome, admin!

    {/if} {/if}
    uses invalid {:if} syntax. {#if loggedIn && isAdmin}

    Welcome, admin!

    {/if}
    uses && logical AND which requires both true (correct).
  3. Final Answer:

    {#if loggedIn && isAdmin} <p>Welcome, admin!</p> {/if} -> Option A
  4. Quick Check:

    Use && for both true conditions [OK]
Hint: Use && to require both conditions true in {#if} [OK]
Common Mistakes:
  • Using || instead of && for both conditions
  • Using bitwise | instead of logical &&
  • Using invalid {:if} syntax
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