Svelte Runes

Svelte 5 introduced runes, a set of special keywords that mark reactive state explicitly. Each rune starts with a dollar sign and looks like a function call. The compiler understands runes directly, so you never import them.

Why Runes Exist

Earlier versions treated every top-level let variable as reactive and relied on the dollar colon label for derived values. That approach worked inside components only. Runes bring the same power to plain JavaScript files with the .svelte.js extension, and they make intent obvious: state looks like state, and derived values look like derived values.

Old syntax still runs in Svelte 5. A component switches to runes mode as soon as it uses a rune. Components in the same project can use different modes.

The Core Runes

Rune          Purpose                          Old Equivalent
------------  -------------------------------  -------------------
$state        Declare reactive data            let count = 0
$derived      Compute from other values        $: doubled = ...
$effect       Run side effects after updates   $: { ... }
$props        Receive data from a parent       export let name
$bindable     Allow two-way prop binding       bind with export let

State

The state rune wraps a starting value. Assigning a new value updates the page.

<script>
  let count = $state(0);
</script>

<button onclick={() => count++}>
  Clicked {count} times
</button>

Runes mode uses plain onclick attributes for events. The on:click directive belongs to the older syntax.

Deep Reactivity

Arrays and objects created with the state rune react to changes inside them. Pushing an item or editing a property updates the page without a reassignment trick.

<script>
  let todos = $state(["Read", "Write"]);
</script>

<button onclick={() => todos.push("Practice")}>Add</button>

{#each todos as todo}
  <p>{todo}</p>
{/each}

Derived Values

The derived rune recalculates when its inputs change. It replaces the dollar colon declaration.

<script>
  let width = $state(10);
  let height = $state(5);
  let area = $derived(width * height);
</script>

<p>Area: {area}</p>

Effects

The effect rune runs code after the page updates. Use it for work outside Svelte, such as logging, timers, or talking to a browser feature. A function returned from the effect cleans up before the next run.

<script>
  let seconds = $state(0);

  $effect(() => {
    const id = setInterval(() => seconds++, 1000);
    return () => clearInterval(id);
  });
</script>

<p>Timer: {seconds}</p>

Props

The props rune receives data from a parent and supports default values through destructuring.

<script>
  let { name, age = 18 } = $props();
</script>

<p>{name} is {age} years old</p>

Runes Family Diagram

            $state (source of truth)
               |          |
               v          v
        $derived       $effect
     (new values)    (side effects)
               |          |
               +----+-----+
                    v
             Page updates on screen

A Layman Analogy

Think of a bakery. The state rune is the ingredient shelf, and the derived rune is the recipe that turns ingredients into a cake. The effect rune is the doorbell that rings after the cake reaches the counter. Use an effect only for the doorbell, never for baking.

Common Mistakes To Avoid

  • Mixing the dollar colon label and runes inside one component
  • Using an effect to calculate a value that a derived rune handles better
  • Calling a rune inside a regular function rather than at the top level
  • Skipping cleanup in effects that start timers

Key Points

  • Runes make reactivity explicit and work in .svelte.js files
  • State holds data, derived computes values, and effect handles side work
  • Objects and arrays react deeply when created with state
  • Older syntax keeps working while you migrate at your own pace