Practice
1. In Svelte, what does the
$: symbol before a statement do?easy
Solution
Step 1: Understand the role of
The$:in Svelte$:prefix marks a statement as reactive, meaning it runs whenever its dependencies change.Step 2: Compare with other options
Options A, B, and D describe unrelated concepts: comments (A), constants (B), and imports (D).Final Answer:
It makes the statement reactive, updating when dependencies change. -> Option BQuick Check:
$:means reactive assignment [OK]
Hint: Remember:
$: triggers updates automatically [OK]Common Mistakes:
- Thinking
$:declares constants - Confusing
$:with comments - Assuming
$:imports modules
2. Which of the following is the correct syntax for a reactive assignment in Svelte?
easy
Solution
Step 1: Identify correct reactive syntax
The correct syntax uses$:followed by the assignment without extra keywords.Step 2: Analyze incorrect options
let $: total = price * quantity; wrongly usesletwith$:. const $total = price * quantity; misusesconstand variable naming. total := price * quantity; uses invalid assignment operator.Final Answer:
$: total = price * quantity; -> Option CQuick Check:
Reactive assignment syntax =$: variable = value;[OK]
Hint: Start reactive lines with
$: and assign normally [OK]Common Mistakes:
- Adding
letorconstbefore$: - Using invalid assignment operators like ':='
- Prefixing variable names with
$
3. Given the Svelte code:
What is the value of
let count = 2; $: double = count * 2; count = 5;
What is the value of
double after these lines run?medium
Solution
Step 1: Understand initial values and reactive update
Initially,countis 2, sodoublebecomes 4 reactively.Step 2: Update
Whencountand trigger reactive assignmentcountchanges to 5, the reactive line recalculatesdouble = 5 * 2 = 10.Final Answer:
10 -> Option DQuick Check:
Reactive updates recalcdouble= 10 [OK]
Hint: Reactive vars update immediately when dependencies change [OK]
Common Mistakes:
- Assuming
doublestays at initial value - Confusing
countvalue withdouble - Ignoring reactive recalculation
4. Identify the error in this Svelte reactive assignment:
let a = 3; $: b = a + c; let c = 4;
medium
Solution
Step 1: Check variable usage order
The reactive assignment usescbefore it is declared, causing an error.Step 2: Validate other options
Options A, B, and C are incorrect because reactive assignments don't requireconst(A), can use variables (B), and semicolons are optional (C).Final Answer:
Variablecis used before declaration. -> Option AQuick Check:
Use variables only after declaration [OK]
Hint: Declare variables before using in reactive assignments [OK]
Common Mistakes:
- Using variables before declaration
- Thinking semicolons are mandatory
- Believing reactive vars must be const
5. You want to keep a database query result reactive in Svelte when the
userId changes. Which reactive assignment correctly updates userData?hard
Solution
Step 1: Understand reactive assignment with async fetch
$: userData = fetch(`/api/user/${userId}`).then(res => res.json()); correctly uses$:to reactively assign a promise from fetch whenuserIdchanges.Step 2: Analyze other options
userData = fetch(`/api/user/${userId}`).then(res => res.json()); lacks reactivity. $: userData = await fetch(`/api/user/${userId}`).then(res => res.json()); usesawaitincorrectly outside async context. $: userData = fetch(`/api/user/${userId}`); assigns fetch promise without parsing JSON.Final Answer:
$: userData = fetch(`/api/user/${userId}`).then(res => res.json()); -> Option AQuick Check:
Use$:with fetch and .then() for reactive async [OK]
Hint: Use
$: with fetch and .then() for reactive async data [OK]Common Mistakes:
- Omitting
$:for reactive update - Using
awaitoutside async function - Not parsing fetch response to JSON
