0
0
Svelteframework~20 mins

Auto-subscription with $ prefix in Svelte - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Svelte Auto-subscription Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this Svelte component?

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?

Svelte
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>
ACount: undefined
BCount: 0
CCount: 3
DCount: NaN
Attempts:
2 left
💡 Hint

Remember that the $ prefix automatically subscribes to the store and updates the value reactively.

📝 Syntax
intermediate
2:00remaining
Which option correctly uses the $ prefix to auto-subscribe a readable store?

Given a readable store user, which of the following Svelte code snippets correctly displays the user's name using auto-subscription?

Svelte
import { readable } from 'svelte/store';

const user = readable({ name: 'Alice' });

&lt;p&gt;User: ???&lt;/p&gt;
A&lt;p&gt;User: {$user.name}&lt;/p&gt;
B&lt;p&gt;User: {$user}.name&lt;/p&gt;
C&lt;p&gt;User: {$user['name']}&lt;/p&gt;
D&lt;p&gt;User: {$user().name}&lt;/p&gt;
Attempts:
2 left
💡 Hint

The $ prefix unwraps the store value directly, so you can access properties on it normally.

🔧 Debug
advanced
2:00remaining
Why does this Svelte component throw an error when using $ prefix?

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?

Svelte
let count = 0;

function increment() {
  count += 1;
}

<button on:click={increment}>Increment</button>
<p>Count: {$count}</p>
ABecause $ prefix only works with Svelte stores, not plain variables
BBecause count is not initialized before use
CBecause the increment function is missing parentheses
DBecause $count needs to be declared as a reactive statement
Attempts:
2 left
💡 Hint

Think about what the $ prefix does in Svelte.

state_output
advanced
2:00remaining
What is the value of 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?

Svelte
import { writable } from 'svelte/store';

const num = writable(5);

let display = '';

$: display = `Number is ${$num}`;

num.set(10);
ANumber is undefined
BNumber is 10
CNumber is $num
DNumber is 5
Attempts:
2 left
💡 Hint

Reactive statements run whenever the store value changes.

🧠 Conceptual
expert
2:00remaining
Which statement about the $ prefix in Svelte is TRUE?

Choose the correct statement about the $ prefix auto-subscription feature in Svelte stores.

AThe $ prefix only works with derived stores, not writable or readable stores
BThe $ prefix can be used to auto-subscribe to any variable declared with let or const
CThe $ prefix requires manual subscription and unsubscription in the component code
DUsing the $ prefix automatically unsubscribes from the store when the component is destroyed
Attempts:
2 left
💡 Hint

Think about how Svelte manages store subscriptions with the $ prefix.