Skip to content
tezvyn:

Svelte's Reactive Assignments with `$: `

Source: svelte.devEasyHow cards are made

Svelte's Reactive Assignments with `$: `

Think of Svelte's $: as a magic label that automatically re-runs code when its inputs change. It's used to create derived values, like $: sum = a + b. The footgun: the compiler only tracks direct dependencies, not those hidden inside function calls.

Why it exists

In UI development, state often depends on other state, like a fullName derived from firstName and lastName. Manually keeping these in sync is tedious and error-prone. Svelte was designed to make reactivity a core language feature, and the $: syntax is its original, compiler-driven solution to remove this boilerplate.

The mental model

Think of the $: label as making a statement "live". It's a contract with the Svelte compiler: "Watch the variables I use in this line. If any of them change, run this line again for me." It turns a simple assignment like sum = a + b into a self-updating declaration, $: sum = a + b.

How it works

During compilation, Svelte scans your <script> block for any top-level statements prefixed with $: . For each one, it identifies the variables that are read (the dependencies). It then generates code that re-executes that statement whenever any of its dependency variables are updated. This happens after the script's initial run but before the component renders. Svelte also topologically sorts these statements, so if : c = b and : b = a, it knows to update b before c when a changes.

When to use it

Use : for simple derived values, like calculating a total from an array of items. It's also used for triggering side effects that depend on state, such as logging a value when it changes (: console.log(count)). You can group multiple statements in a block ($: { ... }) to run them all when a dependency changes. This syntax is the idiomatic way to handle reactivity in Svelte projects not using Svelte 5's newer Runes feature.

When not to use it

Avoid : when dependencies are not immediately visible to the compiler. For example, : doubled = getDouble() will not re-run if a variable inside the getDouble function changes, because the compiler only sees the function call, not its contents. Also, for browser-specific APIs like document, you must wrap the reactive statement in an if (browser) block to prevent errors during server-side rendering. For new Svelte 5+ projects, prefer using derived and effect runes.

One canonical example

To keep a sum variable automatically updated whenever a or b changes, you declare the relationship directly. In your script, you'd have: let a = 1; let b = 2; and then the reactive assignment: $: sum = a + b; In your template, you could display '{a} + {b} = {sum}'. If you later change 'a' or 'b', the displayed sum updates automatically without any extra wiring.

Interview question

What is a critical limitation of Svelte's `$: ` reactive assignments?

  • a.They only track direct variable dependencies, not changes within functions called.Correct
  • b.They cannot be used to trigger side effects like logging.
  • c.They require explicit manual calls to update derived values.
  • d.They are only suitable for server-side rendering, not client-side.
Why?

The card explicitly states that the compiler only tracks direct dependencies and not those hidden inside function calls, which is a key limitation. The card also provides `$: console.log(count)` as an example of using it for side effects, making option B incorrect.

Just read this? Test yourself on what you have been reading.

Read the original → svelte.dev

You just looked this up. Could you explain it out loud?

That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on svelte — each one lists the topics its interview covers.

See open roles