Svelte is a modern JavaScript framework for building user interfaces. Unlike traditional frameworks like React or Vue, Svelte shifts much of the work from runtime to compile time. Instead of using a virtual DOM, Svelte compiles your code into efficient JavaScript that surgically updates the DOM when your application’s state changes.
Svelte is both a framework and a compiler. It takes your declarative components and converts them into highly optimized vanilla JavaScript that directly manipulates the DOM.
Svelte compiles your code to tiny, framework-less vanilla JavaScript, avoiding the overhead of runtime libraries and virtual DOM diffing.
Truly Reactive
Svelte’s reactivity is built into the language, automatically updating the DOM when your application state changes without requiring explicit render calls.
Less Code
Svelte requires significantly less code than other frameworks to achieve the same results, leading to more maintainable applications.
Built-in Transitions
Svelte includes built-in transition and animation capabilities without requiring additional libraries.
No Framework to Download
Since Svelte compiles to vanilla JavaScript, there’s no framework code for users to download, resulting in smaller bundle sizes.
Gentle Learning Curve
Svelte builds on familiar web technologies (HTML, CSS, JavaScript) with minimal new syntax to learn.
The easiest way to start a new Svelte project is using SvelteKit, the official application framework for Svelte, or a simple Svelte template:
Copy
# Using SvelteKit (recommended for full applications)npm create svelte@latest my-svelte-app# Or using a simple Svelte templatenpm create vite@latest my-svelte-app -- --template svelte# Navigate to the project directorycd my-svelte-app# Install dependenciesnpm install# Start the development servernpm run dev
Svelte’s reactivity is based on assignments. When you assign to a variable that’s used in your template, Svelte automatically updates the DOM:
Copy
<script> let count = 0; function increment() { count += 1; // This assignment triggers a DOM update } // Reactive declarations $: doubled = count * 2; $: if (count >= 10) { alert('Count is now ' + count); }</script><button on:click={increment}>{count}</button><p>Doubled: {doubled}</p>
The $: syntax is a JavaScript label that Svelte uses to mark a statement as reactive. Whenever the variables referenced in the statement change, the statement will be re-executed.
<script> let fruits = ['apple', 'banana', 'orange', 'mango'];</script><ul> {#each fruits as fruit, index (fruit)} <li>{index + 1}: {fruit}</li> {/each}</ul><!-- With a key for optimized updates --><ul> {#each fruits as fruit, index (fruit)} <li>{index + 1}: {fruit}</li> {/each}</ul><!-- With an empty state --><ul> {#each fruits as fruit, index (fruit)} <li>{index + 1}: {fruit}</li> {:else} <li>No fruits available</li> {/each}</ul>
Svelte provides lifecycle functions to run code at specific times:
Copy
<script> import { onMount, onDestroy, beforeUpdate, afterUpdate } from 'svelte'; let count = 0; let interval; onMount(() => { // Runs when the component is mounted to the DOM console.log('Component mounted'); interval = setInterval(() => count += 1, 1000); }); onDestroy(() => { // Runs when the component is unmounted from the DOM console.log('Component destroyed'); clearInterval(interval); }); beforeUpdate(() => { // Runs before the DOM is updated console.log('Before update, count is', count); }); afterUpdate(() => { // Runs after the DOM is updated console.log('After update, count is', count); });</script><p>Count: {count}</p>
Svelte provides a built-in store mechanism for managing application state outside of components:
Copy
<!-- stores.js -->import { writable, readable, derived } from 'svelte/store';// Writable store (can be updated from anywhere)export const count = writable(0);// Readable store (can only be updated by the store itself)export const time = readable(new Date(), function start(set) { const interval = setInterval(() => { set(new Date()); }, 1000); return function stop() { clearInterval(interval); };});// Derived store (computed from other stores)export const elapsed = derived( time, $time => Math.round(($time - new Date(0)) / 1000));
Using stores in components:
Copy
<script> import { count } from './stores.js'; function increment() { count.update(n => n + 1); } function decrement() { count.update(n => n - 1); } function reset() { count.set(0); }</script><h1>The count is {$count}</h1><button on:click={increment}>+</button><button on:click={decrement}>-</button><button on:click={reset}>Reset</button>
The $ prefix is a special syntax in Svelte that automatically subscribes to a store and unsubscribes when the component is destroyed.
Svelte encourages accessibility with built-in warnings for common issues:
Copy
<!-- Svelte will warn about missing alt attribute --><img src="image.jpg"> <!-- Warning! --><!-- Correct usage --><img src="image.jpg" alt="Description of image">
Svelte offers a refreshing approach to building user interfaces with its compile-time framework. By shifting work from the browser to the build step, Svelte creates highly optimized applications with less code and better performance. Its intuitive syntax, built-in features like transitions and stores, and growing ecosystem make it an excellent choice for modern web development.With SvelteKit, you get a full-featured application framework that handles routing, server-side rendering, and more, making Svelte suitable for projects of all sizes.
Svelte is still evolving, with Svelte 4 being the current stable version and Svelte 5 in development. The Svelte ecosystem is growing rapidly, with more libraries, tools, and resources becoming available as its popularity increases.