0 day streak
javascript / beginner
Snippet

Reactive Declarations ($:)

Svelte uses the `$:` syntax to mark a statement as reactive. This means the code will automatically re-run whenever any of the variables it references change. It's a powerful way to compute derived state without manual updates.

snippet.js
javascript
1
2
3
4
5
6
<script>
let count = 0;
$: doubled = count * 2;
</script>
<p>{count} doubled is {doubled}</p>
svelte
Breakdown
1
$: doubled = count * 2;
This line tells Svelte to recalculate 'doubled' every time 'count' is updated.