← Back to Lessons Lesson 1 of 8
Beginner getting-started

What is WebAssembly?

What is WebAssembly?

WebAssembly (Wasm) is a binary instruction format that runs in the browser at near-native speed. Think of it as a portable compilation target — you write code in Rust, C++, or Go, compile it to .wasm, and the browser executes it alongside JavaScript.

Why does it exist?

JavaScript is powerful, but it has limits:

  • CPU-intensive tasks (crypto, image processing, physics) are slow in JS
  • Large codebases (games, CAD tools) need predictable performance
  • Existing code in C/C++/Rust can't run in browsers without porting to JS

Wasm solves all three. It's not a replacement for JavaScript — it's a complement.

How it works

┌──────────┐     compile      ┌──────────┐     run       ┌──────────┐
│  Rust    │ ──────────────▶ │  .wasm   │ ────────────▶ │ Browser  │
│  source  │   (wasm-pack)   │  binary  │  (JS import)  │  engine  │
└──────────┘                 └──────────┘               └──────────┘
  1. You write Rust code
  2. wasm-pack compiles it to a .wasm binary + JS glue code
  3. JavaScript imports and calls your Rust functions
  4. The browser runs Wasm at near-native speed

Why Rust?

Many languages can target Wasm, but Rust is the best fit:

  • No garbage collector — Wasm binaries are small (often < 50KB)
  • Memory safety — compiler catches bugs at build time, not at runtime
  • Zero-cost abstractions — high-level code compiles to fast machine code
  • First-class toolingwasm-pack, wasm-bindgen, and web-sys are mature

What Wasm can't do (yet)

  • No direct DOM access — must go through JavaScript bindings
  • No threads by default — SharedArrayBuffer is opt-in
  • No file system access — runs in a sandboxed environment
  • Can't replace JavaScript — it augments it

Try It

The code on the right is plain Rust. Click Run to see it execute. In the next lessons, you'll learn how to make this code run inside the browser via WebAssembly.

Try It

Chapter Quiz

Pass all questions to complete this lesson