Svelte
Svelte Framework
A UI framework that compiles to JavaScript at build time. Manipulates the DOM directly without a Virtual DOM.
Svelte Framework
A UI framework that compiles to JavaScript at build time. Manipulates the DOM directly without a Virtual DOM.
Svelte is a compiler-based UI framework developed by Rich Harris. Unlike React and Vue, it is compiled into pure JavaScript at build time, without a runtime framework. By manipulating the DOM directly without Virtual DOM diffing, it provides a small bundle size and excellent runtime performance.
Svelte's core philosophy is "Less code, more done." Reactivity is supported at the language level, so reactive declarations are possible with just the $ symbol. Simply writing let count = 0; makes a reactive variable, and $: doubled = count * 2; automatically calculates a derived value. Hooks such as useState and useEffect are not needed.
Svelte 5 (2024) introduced a new reactivity system called Runes. With runes such as $state(), $derived(), and $effect(), more explicit reactivity control is possible. Coexisting with the existing $ syntax, it has become easy to reuse reactive logic outside of components as well.
SvelteKit is Svelte's full-stack framework, comparable to Next.js. It provides file-based routing, SSR/SSG/ISR, API Routes, and a layout system. Vercel has hired Rich Harris, the creator of Svelte, and actively supports Svelte.
<!-- Svelte 5 Runes μμ -->
<script lang="ts">
// $state - λ°μν μν
let count = $state(0);
let name = $state('');
let items = $state<string[]>([]);
// $derived - νμ κ° (μλ κ³μ°)
const doubled = $derived(count * 2);
const isValid = $derived(name.length >= 3);
// $effect - μ¬μ΄λ μ΄ννΈ (useEffect λ체)
$effect(() => {
console.log(`Count changed: ${count}`);
// ν΄λ¦°μ
ν¨μ λ°ν κ°λ₯
return () => console.log('Cleanup');
});
// μ΄λ²€νΈ νΈλ€λ¬
function increment() {
count++; // μ§μ λ³κ²½ κ°λ₯
}
function addItem() {
if (name) {
items.push(name); // λ°°μ΄ μ§μ μμ κ°λ₯
name = '';
}
}
// Props μ μ (Svelte 5)
interface Props {
title: string;
onSave?: (data: string) => void;
}
let { title, onSave }: Props = $props();
</script>
<!-- ν
νλ¦Ώ - HTMLκ³Ό κ±°μ λμΌ -->
<div class="container">
<h1>{title}</h1>
<button onclick={increment}>
Count: {count} (doubled: {doubled})
</button>
<input
bind:value={name}
placeholder="μ΄λ¦ μ
λ ₯"
class:invalid={!isValid}
/>
{#if isValid}
<button onclick={addItem}>μΆκ°</button>
{/if}
<ul>
{#each items as item, index (item)}
<li>
{index + 1}. {item}
<button onclick={() => items.splice(index, 1)}>μμ </button>
</li>
{:else}
<li>νλͺ©μ΄ μμ΅λλ€</li>
{/each}
</ul>
</div>
<style>
/* μ»΄ν¬λνΈ μ€μ½ν CSS (μλ μ€μ½ν) */
.container {
padding: 1rem;
}
.invalid {
border-color: red;
}
button {
background: #ff3e00;
color: white;
border: none;
padding: 0.5rem 1rem;
border-radius: 4px;
cursor: pointer;
}
</style>
<!-- μ¬μ¬μ© κ°λ₯ν λ°μν λ‘μ§ (Svelte 5) -->
<!-- lib/counter.svelte.ts -->
<script context="module" lang="ts">
export function createCounter(initial = 0) {
let count = $state(initial);
return {
get count() { return count; },
increment: () => count++,
decrement: () => count--,
reset: () => count = initial
};
}
</script>
When choosing a framework:
"This is an embed widget where bundle size matters. React is over 30KB by default, but with Svelte only the component is bundled, so it's much smaller. We don't need complex state management either, and concise code is an advantage."
In a technical interview:
"Why is Svelte faster than React?" - "There is no Virtual DOM diffing. At compile time, Svelte determines which DOM to update when which variable changes. Because there are no comparison operations at runtime, it's fast, and no framework code is included in the bundle."
In a team tech stack discussion:
"Won't hiring be difficult if we adopt Svelte?" - "The ecosystem is small, but it's easy to learn. If you have React experience, you can adapt in a few days. The syntax is intuitive, so onboarding is actually faster. However, if you need complex libraries, the React ecosystem is more advantageous."
Take a mock interview on this topic β WIA SOOM AI Interview Β· Start for free