0
0
Svelteframework~5 mins

Reactive declarations (let) in Svelte - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a reactive declaration in Svelte using let?
A reactive declaration in Svelte is a way to automatically update a variable when its dependencies change. It uses the $: label before a statement to make it reactive.
Click to reveal answer
beginner
How do you write a reactive declaration that updates total when price or quantity changes?
You write: <br>$: total = price * quantity;<br>This means whenever price or quantity changes, total updates automatically.
Click to reveal answer
intermediate
Can reactive declarations in Svelte run statements other than assignments?
Yes. Reactive declarations can run any statement, like function calls or console logs, as long as they start with $:. For example, $: console.log(count); runs whenever count changes.
Click to reveal answer
beginner
What happens if you use a reactive declaration with a variable that does not change?
The reactive declaration runs once when the component initializes. If the variable never changes, it won't run again.
Click to reveal answer
beginner
Why are reactive declarations useful in Svelte?
They let you write simple code that automatically updates values or runs code when data changes, without manually writing event handlers or watchers.
Click to reveal answer
What symbol starts a reactive declaration in Svelte?
A$:
B#
C@
D!
Which of these will update automatically when count changes?<br>1) $: double = count * 2;<br>2) let double = count * 2;
ANeither 1 nor 2
BOnly 1
CBoth 1 and 2
DOnly 2
Can you use reactive declarations to run a function when a variable changes?
AOnly if the function returns a value
BNo, reactive declarations only work with variable assignments
CYes, by placing the function call after <code>$:</code>
DOnly inside <code>onMount</code>
What happens if a reactive declaration depends on multiple variables?
AIt updates only when the first variable changes
BIt never updates automatically
CIt updates only when all variables change simultaneously
DIt updates when any of those variables change
Which is a correct reactive declaration to log count whenever it changes?
A$: console.log(count);
Blet $: console.log(count);
Cconsole.log($: count);
Dlog$: count;
Explain how reactive declarations work in Svelte and give an example.
Think about how Svelte tracks variables and updates values automatically.
You got /3 concepts.
    Describe a situation where using a reactive declaration is better than manually updating a variable.
    Consider how reactive declarations reduce manual work and bugs.
    You got /3 concepts.