Lesson 1 of 9 beginner 4 min read

Introduction

Anatomy of a Svelte component, superset of HTML, and the fine-grained reactivity mental model.

Interactive Laboratory

Live Runtime

Experiment directly with Introduction concepts in real time

Reactive Dependency Graph
$state(count)
3
$state(multiplier)
2x
$derived()
6
Zero Virtual DOM Diffing
signals.svelte.ts — Runtime Observer Stream
Signal Graph Initialized: [count: 3] -> [derived: 6]
$effect scheduled observer on root component

Introduction to Svelte 5

Svelte is a radical new approach to building user interfaces. Whereas traditional frameworks like React and Vue do the bulk of their work in the browser using a Virtual DOM, Svelte shifts that work into a compile step that happens when you build your app.

Instead of interpreting your application code at runtime, Svelte compiles your components into small, framework-free vanilla JavaScript that surgically updates the DOM when state changes.


The Anatomy of a Svelte Component

A Svelte component is written in a .svelte file using superset HTML syntax. It consists of three primary sections:

<script lang="ts">
	// 1. JavaScript / TypeScript logic & reactive state
	let name = $state('World');
</script>
 
<!-- 2. HTML Markup with Svelte Template Expressions -->
<main class="card">
	<h1>Hello, {name}!</h1>
	<input bind:value={name} placeholder="Enter your name" />
</main>
 
<style>
	/* 3. Component-scoped CSS styles */
	.card {
		padding: 1.5rem;
		border-radius: 1rem;
		background: var(--card);
	}
	h1 {
		color: var(--primary);
	}
</style>

Why Svelte 5 Runes?

In Svelte 5, reactivity is declared explicitly through universal primitives called Runes. Runes start with a dollar sign ($) and work identically inside .svelte components as well as standalone .svelte.ts utility files:

  • $state(): Declares reactive state signals.
  • $derived(): Computes values derived from reactive state.
  • $effect(): Runs side effects when dependencies update.
  • $props(): Declares component input parameters with TypeScript support.

[!TIP] Runes eliminate the distinction between component reactivity and global store reactivity. Everything uses the same universal fine-grained signal system.