Concept Explanation
Svelte is the rebel of the group. React and Vue ship a framework to the browser and do their work while your app runs. Svelte does the opposite: it's a compiler. During your build step it reads your components and writes tiny, hand-optimized vanilla JavaScript — then ships that.
The result: no framework runtime to download, and some of the smallest, fastest apps you can build.
Syntax or Core Structure
A Whole Component
<script>
let count = 0; // just a variable
const increment = () => count += 1;
</script>
<button on:click={increment}>
Clicked {count} times
</button>
<style>
button { padding: 8px 16px; } /* scoped to this component only */
</style>
No useState, no ref — you change a normal variable and Svelte's compiler wires up the DOM update for you.
Key Features
- Zero runtime overhead: no framework code is shipped to the browser.
- Truly reactive assignments:
count += 1is all it takes; the compiler tracks the rest. - Scoped styles & animations: a component's CSS stays in that component, and transitions are built in.
Real World Example
The New York Times uses Svelte for interactive data stories, where every kilobyte and millisecond counts on mobile. Because the output is plain JS, those graphics load fast even on slow connections — exactly where a heavy framework would struggle.
Common Mistakes
- Expecting a virtual DOM: there isn't one. Svelte's model is "compile the framework away," a different mental model from React.
- Mutating arrays in place:
arr.push(x)won't trigger an update. Reassign instead:arr = [...arr, x]. - Skipping SvelteKit: for real apps (routing, data loading), you usually want SvelteKit, not bare Svelte.
Best Practices
- Reassign to react: reactivity keys off assignment (
=), so update state by assigning, not by mutating. - Use SvelteKit for apps: it adds routing, server rendering, and data loading on top of Svelte.
- Lean on scoped styles: keep a component's CSS right in its
.sveltefile — it won't leak out.
Practice Exercises
- The Compiler Quest: Explain in two sentences how "compiling the framework away" differs from React's virtual DOM.
- The Clean Script: Write a 3-line Svelte
<script>that increments a counter reactively on each click. - The File Size: Research and compare the bundle size of a hello-world Svelte app vs a React app.