Derived values with $: let you automatically update one value when others change. It keeps your code simple and reactive without extra work.
Derived values with $: in Svelte
$: derivedValue = expression;
The $: label tells Svelte to recalculate derivedValue whenever any variable in expression changes.
You can use multiple $: statements in one component.
total whenever price or quantity changes.$: total = price * quantity;
fullName automatically.$: fullName = firstName + ' ' + lastName;name changes.$: greeting = `Hello, ${name}!`;This Svelte component shows price and quantity. The total updates automatically when quantity changes. Clicking the button increases quantity and updates total instantly.
<script> let price = 10; let quantity = 2; $: total = price * quantity; function increaseQuantity() { quantity += 1; } </script> <main> <p>Price: {price} USD</p> <p>Quantity: {quantity}</p> <p><strong>Total: {total} USD</strong></p> <button on:click={increaseQuantity} aria-label="Increase quantity">Add one more</button> </main>
Derived values run automatically and efficiently only when needed.
Don't assign to a derived value inside the $: block; it should be computed from other variables.
$: creates reactive derived values that update automatically.
Use it to keep related data in sync without manual updates.
It makes your Svelte code cleaner and easier to read.
Practice
$: label do in Svelte?Solution
Step 1: Understand the role of
The$:in Svelte$:label marks a reactive statement that runs whenever its dependencies change.Step 2: Compare with other options
Options B, C, and D describe unrelated features: constants, imports, and lifecycle functions, not reactive statements.Final Answer:
It creates a reactive statement that updates automatically when dependencies change. -> Option DQuick Check:
Reactive statements = It creates a reactive statement that updates automatically when dependencies change. [OK]
$: means auto-update when data changes [OK]- Thinking
$:declares constants - Confusing
$:with imports or functions - Believing it runs only once
double from count in Svelte?Solution
Step 1: Identify correct reactive syntax
In Svelte, reactive statements start with$:followed by an assignment. So$: double = count * 2;is correct.Step 2: Check other options for syntax errors
let $double = count * 2;wrongly useslet $double, which is invalid.const double = $: count * 2;misplaces$:.double = $count * 2;uses$countwhich is incorrect syntax.Final Answer:
$: double = count * 2; -> Option BQuick Check:
Reactive assignment =$: double = count * 2;[OK]
$: followed by assignment [OK]- Using
$inside variable names - Placing
$:after the variable - Confusing reactive syntax with normal variable declarations
let count = 3; $: double = count * 2; count = 5;
What is the value of
double after count changes?Solution
Step 1: Understand reactive update
Initially,countis 3, sodoubleis 6. Whencountchanges to 5, the reactive statement recalculatesdoubleas 5 * 2 = 10.Step 2: Confirm final value of
After the update,doubledoubleis 10, reflecting the latestcount.Final Answer:
10 -> Option AQuick Check:
double = count * 2 = 10 [OK]
- Assuming
doublestays at initial value - Thinking reactive statements run only once
- Confusing variable assignment timing
let a = 2; $: b = a + 3 $: c = b * 2;
Solution
Step 1: Check syntax and reactive dependencies
In Svelte, semicolons are optional. Reactive statements can depend on other reactive variables likeb.Step 2: Verify variable declarations
Variables can be declared withletand updated reactively. No need forconsthere.Final Answer:
No error; code works correctly. -> Option CQuick Check:
Reactive chaining allowed = No error; code works correctly. [OK]
- Expecting mandatory semicolons
- Believing reactive variables can't chain
- Forcing const declarations unnecessarily
status that shows "High" if score is above 80, "Medium" if between 50 and 80, and "Low" otherwise. Which Svelte code correctly implements this?Solution
Step 1: Check reactive statement syntax
$: status = score > 80 ? 'High' : score >= 50 ? 'Medium' : 'Low';uses a single reactive assignment with nested ternary operators, which is valid and concise.Step 2: Evaluate other options
let status = $: score > 80 ? 'High' : score >= 50 ? 'Medium' : 'Low';incorrectly combinesletand$:.if (score > 80) { status = 'High'; } else if (score >= 50) { status = 'Medium'; } else { status = 'Low'; }uses an if-else block but lacks$:label for reactivity.$: status = (score > 80 && 'High') || (score > 80 && 'Medium') || 'Low';uses logical operators with duplicate conditions that never produce 'Medium'.Final Answer:
$: status = score > 80 ? 'High' : score >= 50 ? 'Medium' : 'Low'; -> Option AQuick Check:
Correct reactive ternary =$: status = score > 80 ? 'High' : score >= 50 ? 'Medium' : 'Low';[OK]
$: for concise reactive logic [OK]- Omitting
$:for reactive statements - Mixing
letwith$:incorrectly - Using if-else without reactive label
Ready to prove it?
Pass the test and this pattern moves unknown
