What is Svelte?

Svelte is a compiler, not a library: a program that translates .svelte files into plain JavaScript at build time (when the app is prepared). The browser never…

React and Vue are libraries (sets of ready-made code) for building web interfaces. If you have tried one of them, you know the pattern: the library's code is sent to the browser, and the library does the work every time data changes. Svelte does it another way. Svelte is a compiler. You write components (pieces of the interface) in .svelte files, which look like plain HTML. Before people use the app, the Svelte compiler translates those files into plain JavaScript that changes the page directly.

The difference is like two ways Mrs. Sri's small food stall could sell iced tea through a delivery app. The first way: every buyer gets tea, sugar, ice, a thermos and a recipe book, and mixes the drink themselves every time they want one. The second way: Mrs. Sri mixes it in her kitchen before the stall opens, and buyers only receive finished iced tea. The buyer is the browser, and mixing is the work of building the interface. React and Vue are like the first way: the ingredients and the recipe (the library's code) are sent to the browser, and the browser does the mixing. Svelte is the second way: that work is done once on your computer, and the browser only receives the finished result. Never used React or Vue? No problem, just remember the two ways of selling iced tea.

A compiler, not a library

A compiler is a program that translates code into other code before that code runs. If you have heard of TypeScript, you already know one example: a .ts file is first translated into .js. Svelte works the same way. The browser never reads a .svelte file. What is sent to the browser is the JavaScript that the file was translated into.

That translation happens at build time, when the code you wrote is turned into files that are ready to send to the browser. A build happens when you prepare the app for people to use (npm run build). It also happens automatically every time you save a file while the dev server runs (npm run dev). The dev server is a program on your computer that shows the app while you work on it.

Here is how that differs from React:

Your first component

One .svelte file is one component. Even plain HTML is a valid Svelte component:

<h1>Hello Svelte!</h1>

The result is one <h1> heading that reads Hello Svelte!. This is also the answer to this lesson's exercise: the code is already in the editor, you only need to run it.

When a component needs data, write JavaScript inside <script>, then show the data with curly braces:

<script>
  let stall = "Mrs. Sri's Stall"
</script>

<h1>Welcome to {stall}!</h1>

In this track we build a small app for Mrs. Sri's stall: a menu (Fried Rice, Chicken Noodles, Iced Tea) and an order cart. Each lesson adds one piece.

The compiled code

Here is the iced tea order component:

<script>
  let portions = $state(0)
</script>

<button onclick={() => portions += 1}>+1 Iced Tea</button>
<p>Portions ordered: {portions}</p>

$state(0) marks portions as data that Svelte watches, starting at 0: every time its value changes, the page changes too. onclick runs portions += 1 every time the button is clicked, and {portions} shows the value. $state and onclick each get a full lesson later: Svelte Reactivity and Event Handling in Svelte.

Before you look at the compiled code, guess. You click the +1 Iced Tea button once. How many elements are rebuilt on the page: 0, 1, or 2?

Here is the JavaScript that the Svelte 5.16 compiler produces from that component (parts not discussed are replaced by comments):

// import ...

var on_click = (_, portions) => $.set(portions, $.get(portions) + 1);
var root = $.template(`<button>+1 Iced Tea</button> <p> </p>`, 1);

export default function Component($$anchor) {
  let portions = $.state(0);
  var fragment = root();
  var button = $.first_child(fragment);

  button.__click = [on_click, portions];

  var p = $.sibling(button, 2);
  var text = $.child(p);

  $.reset(p);
  $.template_effect(() => $.set_text(text, `Portions ordered: ${$.get(portions) ?? ''}`));
  $.append($$anchor, fragment);
}
// ...

Newer Svelte versions write some helpers under other names. Svelte 5.57, for example, writes $.from_html(...) where 5.16 writes $.template(...), and $.delegated('click', button, ...) where 5.16 writes button.__click. $.state, $.get, $.set and $.template_effect are still there, so it works the same way.

So the answer to the guess: one click builds 0 new elements. The only change is one text node, from Portions ordered: 0 to Portions ordered: 1. React also changes only that same text node in the end. But first React runs the component's code again, then compares the old virtual DOM with the new one. Svelte does not need to compare, because the compiler already wrote down which text to replace.

What gets shipped to the browser

The iced tea order component above was built in three ways. The size of each result was measured after the code was minified (made smaller) and compressed with gzip. Both steps are normally done before code is sent to the browser:

Built with JavaScript shipped
Svelte 4.2.19 2.9 KB
Svelte 5.16 9.0 KB
React 19.2 60.4 KB

React always brings react-dom, its engine for comparing virtual DOMs, however small the app is. Svelte only brings the small helper functions that the compiled code actually uses. Svelte 5 brings more helper functions than Svelte 4, but its size is still far below React's. To be fair, there is one note: each Svelte component compiles to more code than its React version (JSX) would. So in a big app, with many components, the gap gets smaller. For small pages, widgets (small components placed on other sites) and landing pages (pages that promote one product), the gap is big.

Svelte 4, Svelte 5, and the exercise editor

Svelte 5 (released October 2024) is the current version, and this track teaches Svelte 5 from the start. Svelte 5 brought a new way of writing called runes: special words starting with $, such as $state and $props, that the compiler recognises. A new project made with the command npx sv create turns runes on for every file in that project, so every file is checked with the Svelte 5 rules.

In older tutorials and repos (collections of project code), you will find Svelte 4 code. It looks different (a prop, in the second row, is data that a component receives from outside):

For Svelte 5 (this track) Older Svelte 4 code
Data that changes let portions = $state(0) let portions = 0
Receiving a prop let { name } = $props() export let name
Click event onclick={...} on:click={...}

Svelte 5 can still run Svelte 4 style components, one component at a time, as long as the component uses no rune at all. But in a project from npx sv create, the compiler rejects the old way of receiving a prop (export let) with the error Cannot use export let in runes mode, unless the component is given the line <svelte:options runes={false} />. How to read Svelte 4 code is covered in the last lesson, Reading Svelte 4 code.

The exercise editor compiles your code with the real Svelte 5 compiler, version 5.16.

Starting your own project

On your computer, a Svelte project is usually created through SvelteKit. SvelteKit is the official framework (a larger toolkit) built on Svelte: it adds routing (moving between pages), a server and the build. Type these commands in a terminal (the window for typing commands):

npx sv create mrs-sri-stall
cd mrs-sri-stall
npm install
npm run dev

⚠️ Three wrong ideas about Svelte

TL;DR: Svelte is a compiler, not a library: a program that translates .svelte files into plain JavaScript at build time (when the app is prepared). The browser never sees a .svelte file. Svelte has no virtual DOM, the picture of the interface that React rebuilds and compares every time data changes. The compiled code builds elements directly, and when data changes it replaces only the text nodes (pieces of text) that depend on that data. In this lesson's example (an iced tea order button), one click means 0 new elements and 1 text replaced. The bundle (the code sent to the browser) is small: 9.0 KB for Svelte 5 and 2.9 KB for Svelte 4, compared with 60.4 KB for React in the same example. Plain HTML such as <h1>Hello Svelte!</h1> is already a valid Svelte component (a piece of the interface). A new project is created with npx sv create. This track teaches Svelte 5 with runes, special words starting with $ ($state, $props), and with onclick for handling clicks. Svelte 4 code from older tutorials still runs in Svelte 5, one component at a time, as long as it is not mixed with runes. But a new project from npx sv create turns runes on for every file, so the compiler rejects some old code, such as export let.

⚡ Svelte is a compiler

Guess first: in Svelte, you write components (pieces of a web interface) in .svelte files. Does the browser ever read those files?

Never. The Svelte compiler, a program that translates code, turns .svelte files into plain JavaScript at build time (when the app is prepared). Only that JavaScript is sent to the browser.

Meaning: React is a library (a set of ready-made code) that is sent to the browser and does its work there. Svelte is not a library but a compiler: its work is done once, on your computer. Think of the iced tea that Mrs. Sri mixes before her stall opens: buyers (the browser) only receive the finished drink (the translated JavaScript).

⚡ Plain HTML is already a component

Write:

<script>
  let stall = "Mrs. Sri's Stall"
</script>

<h1>Welcome to {stall}!</h1>

Meaning: One .svelte file is one component. Plain HTML such as <h1>Hello Svelte!</h1> alone is already a valid component. JavaScript goes inside <script>, and {stall} puts the value of the variable stall on the page.

⚡ No virtual DOM

Write:

<script>
  let portions = $state(0)
</script>

<button onclick={() => portions += 1}>+1 Iced Tea</button>
<p>Portions ordered: {portions}</p>
Guess first: $state(0) marks portions as data that Svelte watches (the page follows its value), and each click adds 1 to it. The button is clicked once. How many elements are rebuilt on the page: 0, 1, or 2?

Zero. Only one text node (a piece of text inside an element) is replaced, from Portions ordered: 0 to Portions ordered: 1.

Meaning: Svelte has no virtual DOM, the picture of the interface that React rebuilds and compares every time data changes. At build time, the Svelte compiler already recorded which text depends on which variable, so nothing needs to be compared in the browser. The code sent to the browser for this component is small too: 9.0 KB, compared with 60.4 KB for React.

What you will learn