In Svelte, you can create reactive statements using the special $: syntax. A reactive statement automatically re-runs whenever any variable it depends on changes. This allows you to perform computations or side effects without manually tracking updates.
The $: label is placed before a statement or block of code.
Svelte automatically tracks the variables used inside the statement.
Whenever a dependent variable changes, the statement re-executes.
This is useful for calculations, logging, or triggering other updates.
In this example, the reactive statement $: double = count * 2 recalculates double whenever count changes. You don't need to call any extra functions — Svelte handles this automatically.
Here, the reactive statement depends on both a and b. Whenever either variable changes, sum is recalculated automatically.
Reactive statements in Svelte make it simple to manage state and calculations without writing additional boilerplate code or manual watchers.