Consider this Svelte component using a writable store and the $ prefix for auto-subscription:
import { writable } from 'svelte/store';
const count = writable(0);
function increment() {
count.update(n => n + 1);
}
<button on:click={increment}>Increment</button>
<p>Count: {$count}</p>What will be displayed in the paragraph after clicking the button three times?
import { writable } from 'svelte/store'; const count = writable(0); function increment() { count.update(n => n + 1); } <button on:click={increment}>Increment</button> <p>Count: {$count}</p>
Remember that the $ prefix automatically subscribes to the store and updates the value reactively.
The $count syntax subscribes to the count store and updates the displayed value whenever the store changes. Clicking the button three times increments the count from 0 to 3, so the paragraph shows "Count: 3".
Given a readable store user, which of the following Svelte code snippets correctly displays the user's name using auto-subscription?
import { readable } from 'svelte/store'; const user = readable({ name: 'Alice' }); <p>User: ???</p>
The $ prefix unwraps the store value directly, so you can access properties on it normally.
Option A correctly uses {$user.name} to access the name property of the store's value. Option A tries to access .name outside the braces, which is invalid. Option A uses bracket notation inside the braces, which is invalid syntax. Option A treats $user as a function call, which is incorrect.
Examine this Svelte component code:
let count = 0;
function increment() {
count += 1;
}
<button on:click={increment}>Increment</button>
<p>Count: {$count}</p>Why does using {$count} cause an error here?
let count = 0; function increment() { count += 1; } <button on:click={increment}>Increment</button> <p>Count: {$count}</p>
Think about what the $ prefix does in Svelte.
The $ prefix in Svelte is a special syntax that auto-subscribes to stores. It does not work with normal variables like count. Since count is a plain number variable, using {$count} causes an error.
display after this code runs?Given this Svelte store and reactive statement:
import { writable } from 'svelte/store';
const num = writable(5);
let display = '';
$: display = `Number is ${$num}`;
num.set(10);What is the final value of display?
import { writable } from 'svelte/store'; const num = writable(5); let display = ''; $: display = `Number is ${$num}`; num.set(10);
Reactive statements run whenever the store value changes.
The reactive statement $: display = `Number is ${$num}`; runs initially with $num as 5, then again after num.set(10) updates the store. So display ends as "Number is 10".
Choose the correct statement about the $ prefix auto-subscription feature in Svelte stores.
Think about how Svelte manages store subscriptions with the $ prefix.
The $ prefix in Svelte automatically subscribes to the store and also handles unsubscribing when the component is destroyed, so you don't have to manage subscriptions manually. It works with writable, readable, and derived stores. It does not work with plain variables.