writable() Store
The writable() store from svelte/store holds a value any component can read and change. It takes an initialValue and returns a store with subscribe, set and update — read reactively in a component with the $store shorthand.
Definition
writable() is the Svelte function that creates a writable store — a small container for a value that lives outside any single component and can be read and changed from anywhere. You import it from svelte/store, call it with an initial value, and get back an object with three methods: subscribe, set and update. It is Svelte's answer to state that must be shared, where a component's private let variable cannot reach.
Stores complement the in-component reactivity of assignment and the $: reactive statement. For the concept of shared state and when to reach for a store, see Svelte reactivity — the basics.
Syntax
<script>
import { writable } from 'svelte/store';
const count = writable(0); // initialValue is 0
</script>
Parameters
| Parameter | Description |
|---|---|
initialValue | The value the store starts with. It can be any type — a number, string, boolean, array or object. It becomes the store's current value until something calls set or update. |
start (optional) | A second argument: a start/stop notifier function called when the store gains its first subscriber, useful for wiring up external sources. Most stores omit it and pass only initialValue. |
The one argument you always pass is initialValue. Provide it directly for a simple default; the optional start notifier exists for stores that need to attach to an outside source when first used.
Return value
writable() returns a store object with exactly three methods:
subscribe(fn)— registersfnto be called immediately with the current value and again after every change. It returns an unsubscribe function you call to stop listening. This is the low-level contract that makes a value a store.set(value)— replaces the store's value withvalueand notifies every subscriber. Use it when the next value does not depend on the current one.update(fn)— passes the current value tofnand stores what it returns, for examplecount.update((n) => n + 1). Use it when the next value is derived from the previous one.- The
$storeshorthand — inside a component, prefixing the store with$(as$count) auto-subscribes: Svelte reads the current value reactively, re-renders on change, and unsubscribes automatically when the component is destroyed. You can also assign to$countdirectly, which Svelte compiles to asetcall.
The $store auto-subscription is why stores feel native rather than bolted on: you almost never call subscribe yourself in a component, because the $ prefix manages the whole subscription lifecycle for you. Outside a component — in a plain .js module — you subscribe manually and hold onto the returned unsubscribe function:
import { writable } from 'svelte/store';
const count = writable(0);
const unsubscribe = count.subscribe((value) => {
console.log('count is now', value);
});
count.set(1); // logs: count is now 1
count.update((n) => n + 1); // logs: count is now 2
unsubscribe(); // stop listening
Examples
A counter store read via $store
The store holds the count; {$count} reads it reactively with the auto-subscription, and update bumps it from its previous value. No subscribe call appears — the $ prefix handles it.
set versus update
update derives the next value from the current one; set replaces it outright. Here add uses update because it counts up from the previous value, while reset uses set because it does not care what the value was.
Browser & runtime support
writable() is part of the svelte/store module in Svelte 3, 4 and 5, so store code written for Svelte 4 keeps working. Alongside it, svelte/store also exports readable for values that should not be set from outside and derived for a store computed from other stores. In Svelte 5 much shared state can also live in runes, but the store contract remains fully supported and is still the right tool for streams and cross-component state.
Frequently asked questions
What does writable() return in Svelte?
subscribe to listen for changes, set to replace the value, and update to derive the next value from the current one. Inside a component you usually read it with the $store shorthand instead of calling subscribe.What is the initialValue argument to writable()?
set or update changes it. An optional second start notifier argument exists for stores that attach to an external source on first subscription.What does the $ prefix on a store do?
$count auto-subscribes to the count store: Svelte reads its value reactively, re-renders on change, and unsubscribes when the component is destroyed. Assigning to $count compiles to a set call.When should I use set versus update?
set(value) when the next value does not depend on the current one, and update((prev) => next) when it does — for example incrementing with count.update((n) => n + 1) so it reads the latest value.