0
0
Svelteframework~5 mins

Reactive statements ($:) in Svelte - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the $: symbol mean in Svelte?
It marks a reactive statement that runs automatically whenever its dependencies change.
Click to reveal answer
beginner
How do reactive statements help in Svelte components?
They update values or run code automatically when related data changes, keeping the UI in sync without manual updates.
Click to reveal answer
beginner
Given <code>let count = 0;</code> and <code>$: doubled = count * 2;</code>, what happens when <code>count</code> changes?
The variable doubled updates automatically to twice the new count value.
Click to reveal answer
intermediate
Can you use multiple reactive statements in one Svelte component?
Yes, you can have many $: statements, each reacting to different variables or expressions.
Click to reveal answer
intermediate
What happens if a reactive statement has no dependencies?
It runs once when the component initializes and never again, because nothing triggers it to rerun.
Click to reveal answer
What triggers a reactive statement marked with $: to run in Svelte?
AWhen any variable it uses changes
BOnly when the component mounts
CWhen the user clicks a button
DWhen the page reloads
Which of these is a correct reactive statement syntax in Svelte?
Alet $: total = price * quantity;
Bfunction $: total() { return price * quantity; }
Cconst $: total = price * quantity;
D$: total = price * quantity;
If you write $: console.log(count);, when will the message print?
AEvery time <code>count</code> changes
BOnly once when the component loads
CNever, because console.log is not reactive
DOnly when a button is clicked
Can reactive statements update other reactive statements in Svelte?
ANo, they run independently
BYes, reactive statements can chain updates
COnly if inside a function
DOnly if declared in the same line
What happens if you write $: alert('Hello'); with no variables?
AAlert never shows
BAlert shows repeatedly causing a loop
CAlert shows once when component loads
DSyntax error
Explain how reactive statements ($:) work in Svelte and why they are useful.
Think about how Svelte updates values without you writing extra code.
You got /4 concepts.
    Describe a simple example using a reactive statement to double a number when it changes.
    Imagine you have a number and want another number always twice it.
    You got /4 concepts.