Consider this Svelte code snippet:
let count = 1; $: doubled = count * 2; count = 3;
What is the value of doubled after the assignment?
let count = 1; $: doubled = count * 2; count = 3;
Reactive assignments update automatically when their dependencies change.
When count changes to 3, the reactive statement recalculates doubled as 3 * 2 = 6.
Which of the following is the correct way to write a reactive assignment in Svelte?
Reactive assignments start with $: followed by the statement.
Option C uses the correct syntax: $: total = price + tax;. Other options are invalid syntax.
Given these reactive assignments:
$: a = x + y; $: b = a * 2; $: c = b + 5;
Which option best reduces unnecessary recalculations?
Grouping related reactive assignments can reduce redundant updates.
Combining related reactive assignments in one block ensures they update together efficiently.
Consider this code:
let count = 0;
$: doubled = count * 2;
function increment() {
count += 1;
}Why might doubled not update when increment() is called?
Check if the function that changes the variable is actually executed.
If increment() is never called, count stays 0, so doubled does not update.
Which of the following best describes what triggers reactive assignments to update in Svelte?
Think about what dependencies reactive statements track.
Svelte tracks variables used inside reactive statements and updates them whenever those variables change.