0
0
Svelteframework~10 mins

Reactive statements ($:) in Svelte - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Reactive statements ($:)
Variable changes
$: reactive statement runs
Update dependent variables or run code
UI updates automatically
Wait for next change
When a variable changes, Svelte runs the reactive statement marked with $: to update dependent values or run code, then updates the UI automatically.
Execution Sample
Svelte
let count = 0;
$: doubled = count * 2;

function increment() {
  count += 1;
}
This code doubles the count reactively whenever count changes.
Execution Table
StepActioncountdoubledUI Update
1Initialize count0undefinedcount=0, doubled not set
2Reactive statement runs00doubled=0 shown
3Call increment()00UI shows count=0, doubled=0
4count += 110count updated, doubled stale
5Reactive statement runs12doubled updated to 2
6UI updates12UI shows count=1, doubled=2
7Call increment()12UI shows count=1, doubled=2
8count += 122count updated, doubled stale
9Reactive statement runs24doubled updated to 4
10UI updates24UI shows count=2, doubled=4
💡 Execution stops after UI updates reflect latest reactive values.
Variable Tracker
VariableStartAfter Step 4After Step 8Final
count0122
doubledundefined024
Key Moments - 3 Insights
Why does the reactive statement run after count changes?
Because Svelte tracks dependencies, when count changes (see step 4 and 8), it reruns the reactive statement (steps 5 and 9) to update doubled.
What happens if you change count without a reactive statement?
Without $:, dependent values like doubled won't update automatically, so UI would show stale values (see step 4 before reactive runs).
Does the reactive statement run on initialization?
Yes, it runs once after initial variable setup (step 2) to set doubled based on count.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of doubled at step 5?
Aundefined
B0
C2
D4
💡 Hint
Check the 'doubled' column at step 5 in the execution_table.
At which step does the UI first show count=1 and doubled=2?
AStep 6
BStep 4
CStep 5
DStep 3
💡 Hint
Look for the 'UI Update' column describing count=1, doubled=2.
If the reactive statement was removed, what would doubled be after incrementing count to 2?
A4
BStale value (not updated)
C2
Dundefined
💡 Hint
Refer to key_moments about reactive statements updating dependent values.
Concept Snapshot
Reactive statements in Svelte use $: prefix.
They run automatically when dependencies change.
Use them to update variables or run code reactively.
UI updates after reactive statements run.
They run once on initialization too.
Full Transcript
In Svelte, reactive statements start with $:. When a variable used inside such a statement changes, Svelte reruns that statement automatically. For example, if you have let count = 0 and a reactive statement $: doubled = count * 2, then whenever count changes, doubled updates too. This happens without extra code. The UI then updates to show the new values. The reactive statement also runs once after initialization to set initial values. This makes it easy to keep variables and UI in sync without manual updates.