Updating Stores: set() and update()
Replace or transform store values using set() and the updater callback in update().
Updating Stores: set() and update() is a free Sveltejs Academy lesson on CoddyKit. This is lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Sveltejs Academy learning path, and your progress syncs across the web and the CoddyKit app. The Sveltejs Academy course includes 4 lessons in total.
Two Update Modes
Stores expose two update methods: set(value) to replace and update(fn) to transform.
set Example
Replace the whole value.
count.set(5);update Example
Transform the current value using a callback.
count.update(n => n + 1);When to Use Which
Use set for absolute changes; use update when the new value depends on the old.
Inside Components
Inside .svelte files, prefer the $store = x shorthand to store.set(x).
Outside Components
In plain JS modules and helpers, call set/update directly.
Object Updates
When the store holds an object, return a new object from update to ensure subscribers see a change.
user.update(u => ({ ...u, name }));Array Updates
Same rule: return a new array, do not mutate in place.
todos.update(list => [...list, newTodo]);Batching
Multiple sequential updates each fire subscribers. For batches, compute the final value and set once.
Async Updates
You can call set inside async callbacks; subscribers receive the new value when it fires.
Custom Setters
Wrap set/update in named functions on a custom store for cleaner APIs.
function increment() { count.update(n => n + 1); }Quick Check
Which method needs the previous value?
Recap
set replaces; update transforms using the current value via a callback.
Learn HTML with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 41
- Lessons
- 163
Frequently Asked Questions
Is the “Updating Stores: set() and update()” lesson free?
Yes — the full text of “Updating Stores: set() and update()” is free to read here on the web. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Sveltejs Academy course, upgrade to CoddyKit PRO. The Sveltejs Academy course includes 4 lessons in total.
What will I learn in “Updating Stores: set() and update()”?
Replace or transform store values using set() and the updater callback in update(). You practise Sveltejs Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Sveltejs Academy?
No prior experience is required. Sveltejs Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 3 of 4.
How long does the “Updating Stores: set() and update()” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Sveltejs Academy lesson?
Yes. Every Sveltejs Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Creating a writable Store
- Subscribing with $ Auto-subscription
- Updating Stores: set() and update()
- readable Stores and Custom Start Functions