Svelte is a compile-time reactive UI framework that compiles components into highly optimized, imperative JavaScript at build time—eliminating the need for a virtual DOM. Unlike traditional frameworks like React or Vue, Svelte shifts work from the browser to the compiler, resulting in smaller bundle sizes and faster runtime performance. Svelte 5, released in October 2024, overhauled reactivity with explicit runes ($state, $derived, $effect) that replace magic $: labels and implicit top-level reactivity. Key mental model: state changes are compiled into surgical DOM updates, not runtime diffing, and in Svelte 5 runes mode on:click is replaced by plain onclick event attributes—making Svelte feel closer to writing vanilla HTML and JavaScript while still offering components, stores, transitions, and the full SvelteKit framework.
What This Cheat Sheet Covers
This topic spans 26 focused tables and 197 indexed concepts. Below is a complete table-by-table outline of this topic, spanning foundational concepts through advanced details.
Table 1: Component Basics
A Svelte component is a .svelte file containing a <script> block, HTML markup, and an optional <style> block. Understanding the difference between Svelte 4's implicit top-level reactivity and Svelte 5's explicit rune syntax is the first decision every Svelte developer must make when starting a new project.
| Concept | Example | Description |
|---|---|---|
<script> let count = $state(0);</script><h1>{count}</h1> | A .svelte file contains <script>, markup, and optional <style> — all scoped to the component by default. | |
let count = $state(0);count += 1; | Use $state() in Svelte 5 to declare reactive state — reassignment and mutations trigger DOM updates. | |
let count = 0;count += 1; | Top-level let declarations are automatically reactive in Svelte 4 legacy mode — reassignment triggers updates. | |
let { name, age = 18 } = $props(); | Use $props() in Svelte 5 to declare props — supports destructuring and default values. | |
<button onclick={() => count++}> +1</button> | In runes mode, use plain HTML event attributes ( onclick, oninput, etc.) — no on: directive needed. |