A writable store is a special object in Svelte that holds a value you can read and change. It lets components share and react to data changes easily.
Click to reveal answer
beginner
How do you create a writable store in Svelte?
Use the <code>writable</code> function from <code>svelte/store</code>. For example: <br><code>import { writable } from 'svelte/store';<br>const count = writable(0);</code>
Click to reveal answer
beginner
How do you update the value of a writable store?
You can use the set method to replace the value or the update method to change it based on the current value. Example: count.set(5); count.update(n => n + 1);
Click to reveal answer
beginner
How do components react to changes in a writable store?
Components subscribe to the store. When the store value changes, subscribed components automatically update their displayed data.
Click to reveal answer
intermediate
What is the difference between subscribe and set in writable stores?
subscribe lets you listen to changes in the store value. set changes the store value. Subscribe is for reading/reacting, set is for writing.
Click to reveal answer
Which function creates a writable store in Svelte?
Astore()
Breadable()
Cwritable()
Dderived()
✗ Incorrect
The writable() function creates a store you can both read and write.
How do you change the value of a writable store?
AUsing set() or update() methods
BUsing subscribe() method
CDirectly assigning a new value
DUsing get() method
✗ Incorrect
set() replaces the value, update() changes it based on current value.
What does the subscribe() method do in writable stores?
ACreates a new store
BChanges the store value
CDeletes the store
DListens for changes in the store value
✗ Incorrect
subscribe() lets components react when the store value changes.
Which import is needed to use writable stores in Svelte?
Aimport { writable } from 'svelte/store';
Bimport { writable } from 'svelte';
Cimport writable from 'svelte/store';
Dimport { store } from 'svelte/store';
✗ Incorrect
Writable stores come from the 'svelte/store' module.
If you want to increase a count store by 1, which method is best?
Acount.set(count + 1);
Bcount.update(n => n + 1);
Ccount.subscribe(n => n + 1);
Dcount.get() + 1;
✗ Incorrect
update() safely changes the value based on current state.
Explain how writable stores help manage shared state in Svelte applications.
Think about how multiple parts of an app can see and change the same data.
You got /4 concepts.
Describe the difference between the set() and update() methods on writable stores.
One method needs the new value, the other uses a function to compute it.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of a writable store in Svelte?
easy
A. To style components dynamically
B. To hold data that can be changed and shared across components
C. To create static, unchangeable data
D. To handle routing between pages
Solution
Step 1: Understand writable store role
Writable stores in Svelte are designed to hold data that can be changed and shared reactively.
Step 2: Compare options
Only To hold data that can be changed and shared across components correctly describes writable stores as changeable and shareable data holders.
Final Answer:
To hold data that can be changed and shared across components -> Option B
Quick Check:
Writable store = changeable shared data [OK]
Hint: Writable stores hold changeable shared data [OK]
Common Mistakes:
Confusing writable stores with static data
Thinking writable stores handle styling or routing
Assuming writable stores cannot be updated
2. Which of the following is the correct way to create a writable store in Svelte?
easy
A. import { writable } from 'svelte/store'; const count = writable(0);
B. import { readable } from 'svelte/store'; const count = readable(0);
C. const count = writable(0); without import
D. import { store } from 'svelte'; const count = store(0);
Solution
Step 1: Check import statement
The writable store must be imported from 'svelte/store' using { writable }.
Step 2: Verify store creation syntax
Creating a writable store uses writable(initialValue), so import { writable } from 'svelte/store'; const count = writable(0); is correct.
Final Answer:
import { writable } from 'svelte/store'; const count = writable(0); -> Option A
Quick Check:
Writable store import and creation = import { writable } from 'svelte/store'; const count = writable(0); [OK]
Hint: Writable stores need import { writable } from 'svelte/store' [OK]
Common Mistakes:
Using readable instead of writable
Forgetting to import writable
Wrong import path or syntax
3. Given this code:
import { writable } from 'svelte/store';
const count = writable(5);
count.update(n => n + 3);
console.log($count);
What will be logged to the console?
medium
A. Error: $count is undefined
B. 8
C. 3
D. 5
Solution
Step 1: Understand store update
The update method changes the store value from 5 to 8 by adding 3.
Step 2: Check usage of $count outside component
The $ prefix to access store value works only inside Svelte components, not in plain JS code like console.log.
Final Answer:
Error: $count is undefined -> Option A
Quick Check:
$ prefix only works inside components [OK]
Hint: Use $ only inside components, not in plain JS [OK]
Common Mistakes:
Assuming $count works outside components
Ignoring that update changes value
Expecting console.log to show store value directly
D. count + 1 is invalid because count is a store, not a number
Solution
Step 1: Identify what count represents
count is a writable store object, not a number, so count + 1 is invalid.
Step 2: Correct way to update store
You must use count.update(n => n + 1) or count.set(value) with a number, not count + 1.
Final Answer:
count + 1 is invalid because count is a store, not a number -> Option D
Quick Check:
Store object + number is invalid [OK]
Hint: Use update or get value before set, not store + number [OK]
Common Mistakes:
Trying to add number directly to store object
Forgetting to use update for changes
Assuming set needs import separately
5. You want to create a writable store that holds a number starting at 10. Then you want to double its value only if it is less than 50. Which code correctly does this?
hard
A. const num = writable(10);
num.set(20);
B. const num = writable(10);
num.set(n => n < 50 ? n * 2 : n);
C. const num = writable(10);
num.update(n => n < 50 ? n * 2 : n);
D. const num = writable(10);
num.update(n => n * 2);
Solution
Step 1: Create writable store with initial value 10
Using writable(10) correctly sets the initial number.
Step 2: Use update with condition to double if less than 50
update receives current value n and returns n*2 if n < 50, else n unchanged, matching requirement.
Step 3: Check other options
const num = writable(10);
num.set(n => n < 50 ? n * 2 : n); wrongly uses set with a function, which is invalid. const num = writable(10);
num.update(n => n * 2); doubles unconditionally. const num = writable(10);
num.set(20); sets fixed 20 ignoring condition.
Final Answer:
const num = writable(10);
num.update(n => n < 50 ? n * 2 : n); -> Option C
Quick Check:
Use update with condition to change value [OK]
Hint: Use update(fn) with condition to change store value [OK]
Common Mistakes:
Using set with a function instead of a value
Not applying condition inside update
Setting fixed value ignoring condition
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.