0
0
Svelteframework~10 mins

Derived values with $: in Svelte - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Derived values with $:
Declare reactive variable with $:
Evaluate dependencies
Update derived value
Reflect changes in UI
Wait for next dependency change
Back to Evaluate dependencies
Svelte watches variables used in $: reactive statements and updates derived values automatically when dependencies change.
Execution Sample
Svelte
let count = 1;
$: doubled = count * 2;

function increment() {
  count += 1;
}
This code defines a reactive derived value 'doubled' that updates automatically when 'count' changes.
Execution Table
StepActioncountdoubledUI Update
1Initialize count=11undefinedNo update yet
2Evaluate $: doubled = count * 212Display doubled=2
3Call increment()12No immediate UI change
4Update count += 122No UI update yet
5Re-evaluate $: doubled = count * 224Display doubled=4
6Wait for next change24UI stable
7Call increment()24No immediate UI change
8Update count += 134No UI update yet
9Re-evaluate $: doubled = count * 236Display doubled=6
10Wait for next change36UI stable
💡 No more changes; reactive updates stop until count changes again
Variable Tracker
VariableStartAfter Step 4After Step 8Final
count1233
doubledundefined246
Key Moments - 2 Insights
Why doesn't the UI update immediately when increment() changes count?
Because the reactive statement $: doubled = count * 2 runs after count changes, updating 'doubled' and then the UI. See steps 3-5 in execution_table.
What triggers the reactive statement to run again?
Any change to variables used inside the $: statement triggers re-evaluation. Here, changing 'count' triggers recalculation of 'doubled' (steps 4 and 8).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of 'doubled' after step 2?
A1
B2
Cundefined
D4
💡 Hint
Check the 'doubled' column in row for step 2 in execution_table
At which step does 'count' first become 3?
AStep 8
BStep 9
CStep 4
DStep 10
💡 Hint
Look at the 'count' column in execution_table rows for steps 4 and 8
If we remove the $: from 'doubled', what happens to the UI updates?
AUI updates 'doubled' only once at start
BUI updates 'doubled' automatically
CUI never updates 'doubled' after count changes
DUI updates 'doubled' twice per count change
💡 Hint
Reactive statements with $: cause automatic updates; without it, 'doubled' won't recalc (see concept_flow)
Concept Snapshot
Svelte's $: syntax creates reactive derived values.
Whenever a dependency changes, the $: statement re-runs.
This updates variables and UI automatically.
Use $: for values based on other reactive variables.
No manual event or watcher needed.
Full Transcript
In Svelte, the $: label marks a reactive statement. When variables used inside it change, Svelte re-runs that statement to update derived values. For example, if 'count' changes, then 'doubled = count * 2' recalculates automatically. This triggers the UI to update with the new value. The execution table shows how 'count' starts at 1, 'doubled' is calculated as 2, then when 'count' increments, 'doubled' updates accordingly. This reactive system saves you from writing manual update code and keeps UI in sync with data.