Web Dev Academy JavaScript frameworks · Svelte Tool 13 / 64
JavaScript frameworks

Svelte

A compiler, not a runtime. Svelte turns your components into tiny, surgical JavaScript at build time — there is no virtual DOM and no framework shipped to the browser.

!
Svelte compiles ahead-of-time, so there's no script you can drop in via CDN like React or Vue. The interactive demos below are a faithful vanilla-JS simulation of Svelte's reactivity — but every code block shows the real Svelte component code that produces the same behaviour.
Demo 01

Reactive assignments — a counter

In Svelte you just reassign a variable (count += 1) and the DOM updates. No setState, no hooks. The simulation below mimics exactly that.

● Simulation
0
<!-- Counter.svelte (real Svelte) -->
<script>
  let count = 0;
</script>

<button on:click={() => count -= 1}>–</button>
<span>{count}</span>
<button on:click={() => count += 1}>+</button>
Demo 02

Reactive declarations with $:

The $: label marks a value that re-computes whenever its inputs change — Svelte's elegant take on derived state. Here doubled tracks the counter.

● Simulation
count = 0  →  doubled = 0
<script>
  let count = 0;
  $: doubled = count * 2;   // reactive declaration
</script>

<p>{count} → {doubled}</p>
Demo 03

Reactive total over a list

A $: block can sum a whole array. Change any quantity and the total recomputes — just like Svelte would after the compile step.

● Simulation
Total: €0.00
<script>
  let cart = [
    { name: 'Coffee', price: 3.5, qty: 2 },
    { name: 'Croissant', price: 2.2, qty: 1 }
  ];
  $: total = cart.reduce((s, i) => s + i.qty * i.price, 0);
</script>

{#each cart as item}
  <input type="number" bind:value={item.qty}>
{/each}
<p>Total: {total.toFixed(2)}</p>
Demo 04

Svelte stores — shared state

A writable store holds state any component can subscribe to. Both badges below read the same store, so they always match.

● Simulation
Badge A: 0
Badge B: 0
// store.js
import { writable } from 'svelte/store';
export const count = writable(0);

<!-- any component -->
<script> import { count } from './store.js'; </script>
<button on:click={() => $count += 1}>{$count}</button>
i
The demos on this page are a vanilla-JavaScript simulation of Svelte's reactivity (reactive assignments, $: declarations and stores) because Svelte compiles ahead-of-time and has no CDN runtime. Every code block shows the real Svelte component source. The shared CSS shell provides page styling.

↑ All 64 tools