Bird
Raised Fist0
Svelteframework~20 mins

Array and object reactivity gotchas in Svelte - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Svelte Reactivity Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
Why does this Svelte component not update the list display?

Consider this Svelte component that tries to add an item to an array and show the updated list:

  <script>
    let items = ['apple', 'banana'];
    function addItem() {
      items.push('orange');
    }
  </script>

  <button on:click={addItem}>Add Orange</button>
  <ul>
    {#each items as item}
      <li>{item}</li>
    {/each}
  </ul>

Why does clicking the button not update the displayed list?

Svelte
  <script>
    let items = ['apple', 'banana'];
    function addItem() {
      items.push('orange');
    }
  </script>

  <button on:click={addItem}>Add Orange</button>
  <ul>
    {#each items as item}
      <li>{item}</li>
    {/each}
  </ul>
ABecause Svelte does not detect changes when mutating arrays directly; the array reference must change to trigger reactivity.
BBecause the push method is asynchronous and the UI updates before the item is added.
CBecause the items array is declared with let instead of const, so Svelte ignores changes.
DBecause the {#each} block requires a key to detect changes in the array.
Attempts:
2 left
💡 Hint

Think about how Svelte tracks changes to variables and when it updates the UI.

❓ state_output
intermediate
2:00remaining
What is the value of 'user.name' after this update in Svelte?

Given this Svelte code:

  <script>
    let user = { name: 'Alice', age: 30 };
    function updateName() {
      user.name = 'Bob';
    }
  </script>

  <button on:click={updateName}>Change Name</button>
  <p>Name: {user.name}</p>

After clicking the button, what will be displayed as the name?

Svelte
  <script>
    let user = { name: 'Alice', age: 30 };
    function updateName() {
      user.name = 'Bob';
    }
  </script>

  <button on:click={updateName}>Change Name</button>
  <p>Name: {user.name}</p>
AThe component will throw an error
BBob
Cundefined
DAlice
Attempts:
2 left
💡 Hint

Consider how Svelte detects changes in object properties.

📝 Syntax
advanced
2:00remaining
Which option correctly updates an array reactively in Svelte?

Choose the code snippet that correctly updates the todos array so Svelte reacts and updates the UI:

Atodos.push('new task');
Btodos = [...todos, 'new task'];
Ctodos = todos.concat('new task');
Dtodos[todos.length] = 'new task';
Attempts:
2 left
💡 Hint

Remember that Svelte needs the variable reference to change to detect updates.

🔧 Debug
advanced
2:00remaining
Why does this Svelte code fail to update the UI when changing an object property?

Look at this Svelte snippet:

  <script>
    let profile = { username: 'user1', status: 'active' };
    function deactivate() {
      profile.status = 'inactive';
      profile = profile;
    }
  </script>

  <button on:click={deactivate}>Deactivate</button>
  <p>Status: {profile.status}</p>

Even though profile = profile; is used, the UI does not update. Why?

Svelte
  <script>
    let profile = { username: 'user1', status: 'active' };
    function deactivate() {
      profile.status = 'inactive';
      profile = profile;
    }
  </script>

  <button on:click={deactivate}>Deactivate</button>
  <p>Status: {profile.status}</p>
ABecause assigning the same object reference does not trigger reactivity in Svelte.
BBecause the status property is read-only and cannot be changed.
CBecause the function deactivate is not called correctly on button click.
DBecause Svelte requires a special method to update nested object properties.
Attempts:
2 left
💡 Hint

Think about how Svelte detects changes to variables and their references.

🧠 Conceptual
expert
2:00remaining
Which statement best explains Svelte's reactivity with arrays and objects?

Choose the most accurate explanation about how Svelte tracks changes in arrays and objects for UI updates.

ASvelte requires using special reactive stores to track changes in arrays and objects.
BSvelte tracks changes by comparing deep equality of arrays and objects on every update.
CSvelte only tracks changes when the variable reference changes; mutating properties or elements directly does not trigger updates.
DSvelte automatically tracks all nested property changes in objects and arrays without extra code.
Attempts:
2 left
💡 Hint

Recall how Svelte's compiler detects changes to variables.

Practice

(1/5)
1. In Svelte, why does directly modifying an array element like arr[0] = 10 not update the UI?
easy
A. Because Svelte does not support arrays at all.
B. Because arrays are immutable in Svelte.
C. Because you must use a special method to update arrays in Svelte.
D. Because Svelte only tracks assignments to the whole variable, not internal mutations.

Solution

  1. Step 1: Understand Svelte's reactivity model

    Svelte updates the UI only when a variable is assigned a new value, not when its internal parts change.
  2. Step 2: Analyze direct array element modification

    Changing arr[0] = 10 mutates the array but does not assign a new array to arr, so Svelte does not detect the change.
  3. Final Answer:

    Because Svelte only tracks assignments to the whole variable, not internal mutations. -> Option D
  4. Quick Check:

    Direct mutation ≠ UI update [OK]
Hint: Always reassign arrays to trigger updates in Svelte [OK]
Common Mistakes:
  • Thinking direct mutations trigger updates
  • Believing arrays are immutable in Svelte
  • Using methods without reassignment
2. Which of the following is the correct way to update an object property reactively in Svelte?
easy
A. obj = { ...obj, key: 'value' };
B. obj.key = 'value';
C. obj.key = 'value'; obj.update();
D. Object.assign(obj, { key: 'value' });

Solution

  1. Step 1: Recall Svelte's reactivity for objects

    Svelte detects changes only when the whole object variable is reassigned.
  2. Step 2: Evaluate each option

    obj = { ...obj, key: 'value' }; uses spread syntax to create a new object and assign it to obj, triggering reactivity. Direct property assignment like obj.key = 'value'; or Object.assign(obj, { key: 'value' }); mutate the object without reassignment, and obj.key = 'value'; obj.update(); calls a non-existent method.
  3. Final Answer:

    obj = { ...obj, key: 'value' }; -> Option A
  4. Quick Check:

    Reassign object with spread = reactive update [OK]
Hint: Use spread syntax and reassign objects for reactivity [OK]
Common Mistakes:
  • Mutating object properties directly
  • Expecting methods like obj.update() to exist
  • Using Object.assign without reassignment
3. Given the code:
let numbers = [1, 2, 3];
numbers.push(4);

What will Svelte render if you display {numbers.join(',')} immediately after?
medium
A. 1,2,3,4
B. 1,2,3
C. Error: numbers is not reactive
D. 4

Solution

  1. Step 1: Understand array mutation with push()

    The push() method changes the array but does not reassign it.
  2. Step 2: Check Svelte's reactivity trigger

    Since numbers is not reassigned, Svelte does not detect the change and the UI remains with the old array.
  3. Final Answer:

    1,2,3 -> Option B
  4. Quick Check:

    Array mutation without reassignment = no UI update [OK]
Hint: Reassign array after mutation to update UI [OK]
Common Mistakes:
  • Assuming push triggers UI update
  • Expecting automatic reactivity on mutations
  • Confusing errors with silent no-update
4. You have this Svelte code:
let user = { name: 'Alice', age: 25 };

function birthday() {
  user.age += 1;
}

Why does calling birthday() not update the UI showing {user.age}?
medium
A. Because the age property is read-only in Svelte.
B. Because Svelte does not support objects with numeric properties.
C. Because the function does not reassign the user object after mutation.
D. Because the function is missing an await keyword.

Solution

  1. Step 1: Analyze mutation inside birthday()

    The function increments user.age directly, mutating the object.
  2. Step 2: Check reactivity requirements

    Svelte requires reassignment of the whole object variable to detect changes; direct mutation alone does not trigger UI updates.
  3. Final Answer:

    Because the function does not reassign the user object after mutation. -> Option C
  4. Quick Check:

    Direct mutation without reassignment = no UI update [OK]
Hint: Always reassign objects after changing properties [OK]
Common Mistakes:
  • Thinking numeric properties are special
  • Assuming await is needed for sync code
  • Believing direct mutation triggers UI update
5. How can you correctly update the first element of an array items reactively in Svelte?
hard
A. items = [...items]; items[0] = 'new';
B. items = ['new', ...items.slice(1)];
C. items[0] = 'new'; items = items;
D. items = items; items[0] = 'new';

Solution

  1. Step 1: Understand reactivity with arrays

    Direct mutation like items[0] = 'new' does not trigger updates unless followed by reassignment.
  2. Step 2: Evaluate each option

    items = ['new', ...items.slice(1)]; creates a new array with the first element replaced, triggering reactivity correctly. items = [...items]; items[0] = 'new'; creates a shallow copy of the array first, then mutates the first element, which is reactive because the reassignment happens before mutation and the array reference changes. items[0] = 'new'; items = items; mutates first then reassigns the same array reference, which may not trigger reactivity reliably. items = items; items[0] = 'new'; reassigns the same array reference then mutates, which does not trigger reactivity.
  3. Final Answer:

    items = [...items]; items[0] = 'new'; -> Option A
  4. Quick Check:

    Reassign array with spread then mutate = reactive update [OK]
Hint: Reassign array with spread before mutation for reactivity [OK]
Common Mistakes:
  • Mutating after reassignment
  • Reassigning without changes
  • Mutating before reassignment expecting update
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