0Pricing
Sveltejs Academy · Lesson

Creating a writable Store

Create a writable store and initialize it with a starting value.

Creating a writable Store is a free Sveltejs Academy lesson on CoddyKit — lesson 1 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, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Why Stores

Stores hold reactive state that can be shared across components. They are useful for global state like the current user.

Import writable

The writable factory comes from svelte/store.

import { writable } from "svelte/store";

Basic Store

Pass an initial value to create a store.

export const count = writable(0);

Object Initial Value

Stores can hold any value: numbers, strings, arrays, objects.

export const user = writable({ name: "", email: "" });

Store API

Every store has three methods: subscribe, set, and update.

Set Method

Replace the value entirely.

count.set(10);

Update Method

Transform the current value with a callback.

count.update(n => n + 1);

Module File

Stores are typically declared in a shared module file like src/lib/stores.js and imported elsewhere.

Static Initial

The initial value can also be loaded from localStorage or a default.

const init = JSON.parse(localStorage.getItem("count") ?? "0");
export const count = writable(init);

No Re-Creation

Each writable() call creates a new store. Share the same instance across imports.

Naming Conventions

Pick clear names like currentUser. Stores often end with their domain noun.

Quick Check

Which method replaces the value?

Recap

Create writable stores with writable(initial). They expose subscribe, set, and update.

Frequently asked questions

Is the “Creating a writable Store” lesson free?

Yes — the full text of “Creating a writable Store” is free to read here on the web, and the Sveltejs Academy course includes 4 lessons in total. 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.

What will I learn in “Creating a writable Store”?

Create a writable store and initialize it with a starting value. 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; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Creating a writable Store” 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

  1. Creating a writable Store
  2. Subscribing with $ Auto-subscription
  3. Updating Stores: set() and update()
  4. readable Stores and Custom Start Functions
← Back to Sveltejs Academy