WebAssembly Tutorial
Learn WebAssembly step by step: 38 lessons in 9 chapters with examples, quizzes, diagrams and projects.
Start learning →
of lessons completed
Getting Started
- What Is WebAssembly? Learn what WebAssembly (Wasm) is, why browsers and servers run it, how it differs from JavaScript, what it is good at, and where its limits are.
- How WebAssembly Runs Follow a .wasm file from download to running code. Learn the decode, validate, compile and instantiate steps, the sandbox, hosts, and why validation matters.
- Your First Wasm Module Build and run a real WebAssembly module from scratch. Learn the type, function, export and code sections, plus a tiny JavaScript byte builder used in this course.
- The WebAssembly JavaScript API A tour of the WebAssembly JavaScript API. Use validate, compile, instantiate, Module, Instance and streaming loading in browsers and Node.js, with runnable examples.
- Value Types and Numbers Understand the four number types of WebAssembly, signed versus unsigned integers, i64 and BigInt, float rounding, conversions, and the other value types.
Modules, Memory and Tables
- Module Structure and Sections Learn every section of a WebAssembly binary module, their order, index spaces, the start function and custom sections, with a runnable section dumper.
- Linear Memory Learn how WebAssembly linear memory works. Create a Memory in JavaScript, share it with a module, read and write bytes, grow it safely and handle out-of-bounds traps.
- Data Segments and Strings Learn how WebAssembly stores strings and constant data. Use active and passive data segments, UTF-8 with TextEncoder and TextDecoder, and memory.init with data.drop.
- Tables and Indirect Calls Learn how WebAssembly tables hold function references, how call_indirect implements function pointers and virtual methods, and how to manage tables from JavaScript.
- WebAssembly Globals Learn WebAssembly global variables. Declare mutable and immutable globals, use global.get and global.set, and share them with JavaScript through WebAssembly.Global.
The Text Format
- WebAssembly Text Format (WAT) Basics Learn the WebAssembly text format (WAT): S-expressions, modules, functions, flat and folded instructions, names, comments, and how to turn WAT into a .wasm file.
- The Stack Machine and Instructions Learn how WebAssembly executes code on a stack machine. Trace instructions, use locals and local.tee, arithmetic, bitwise and comparison operators, select, and drop.
- Control Flow in WebAssembly Learn structured control flow in WebAssembly text format: if/else, block, loop, br, br_if, br_table, return and unreachable, with factorial, gcd and prime examples.
- Functions, Locals and Calls Learn WebAssembly functions: signatures, call, recursion, mutual recursion, multiple return values, early return, exports by name, and what happens on stack overflow.
- Memory Instructions Learn the WebAssembly memory instructions: load and store variants, offset and alignment, memory.size, memory.grow, memory.fill and memory.copy, with array and string examples.
Calling Between JS and Wasm
- Exporting Functions to JavaScript Learn how JavaScript calls WebAssembly exports: argument and result conversion for i32, i64, f32 and f64, multi-value, exported memories, tables and globals, and call overhead.
- Importing JavaScript Functions Learn how WebAssembly modules import JavaScript functions: the import object, console.log and Math imports, callbacks, LinkError, re-entrancy and exceptions across the boundary.
- Passing Strings and Arrays Learn the pointer and length pattern for strings and arrays in WebAssembly: a bump allocator, TextEncoder, typed array views, returning strings, and the stale-view pitfall.
- Traps and Errors Learn every way WebAssembly can fail: CompileError, LinkError, RuntimeError traps, stack overflow, out of memory, and how to catch them. Includes the exception handling Tag and Exception API.
Compiling from C, Rust and Others
- Compiling C to WebAssembly with Emscripten Learn how to compile C code to WebAssembly with Emscripten - emcc flags, exported functions, cwrap, malloc and memory, plus runnable examples of what the compiler produces.
- Compiling Rust to WebAssembly with wasm-bindgen Learn the Rust to WebAssembly workflow - targets, wasm-pack, wasm-bindgen glue, structs and Result types across the boundary - with runnable stand-ins for generated code.
- Other Languages for WebAssembly Compare the languages that compile to WebAssembly - AssemblyScript, Go and TinyGo, Zig, C# Blazor, Kotlin, Swift - with runnable BigInt and feature-detection examples.
- Passing Strings and Structs Across the Boundary Learn how strings, arrays and structs cross the JavaScript and WebAssembly boundary - UTF-8 encoding, pointer and length pairs, allocation, alignment and endianness, with runnable code.
Performance and Debugging
- WebAssembly Performance Learn when WebAssembly is faster than JavaScript, how to benchmark fairly, why boundary crossings and copies cost time, and how startup, streaming and tiering affect real pages.
- Debugging WebAssembly Learn to debug WebAssembly - reading traps, the name section and stack traces, DWARF debug info and source maps in browser DevTools, logging from wasm, and a checklist for common bugs.
- Binary Size and Optimization Learn how WebAssembly binaries are laid out, why LEB128 keeps them small, and how wasm-opt, stripping, LTO, twiggy and Brotli compression shrink modules for faster page loads.
- SIMD and Threads in WebAssembly Learn WebAssembly SIMD (v128 vectors) and threads (shared memory, atomics, Web Workers) with runnable examples, plus cross-origin isolation, feature detection and when each one pays off.
WASI and Beyond the Browser
- What is WASI? The WebAssembly System Interface Understand WASI, the WebAssembly System Interface - capabilities, preopened directories, fd_write, args and environment, preview1 vs preview2 - with runnable Node WASI and hand-written host examples.
- Running WebAssembly Outside the Browser Learn how to run WebAssembly on servers and the command line - Wasmtime, Wasmer, WasmEdge, wazero, Node.js and Deno - with args, environment, preopens, exit codes and embedding examples.
- The WebAssembly Component Model and WIT Learn the WebAssembly Component Model - WIT interfaces and worlds, the canonical ABI that lifts and lowers strings and records, composition, wasm-tools, jco and cargo-component - with runnable stand-ins.
- WebAssembly on Servers and the Edge Learn why WebAssembly suits servers, serverless and edge platforms - fast cold starts, per-request isolation, plugins and proxies - with runnable instance-isolation and memory-limit examples.
Patterns and Tooling
- The WebAssembly Toolchain Tour the WebAssembly toolchain - wat2wasm, wasm-objdump, wasm-opt, wasm-tools, twiggy, wasm-pack - then build a module byte by byte and write a mini section inspector in JavaScript.
- Bundlers and Loading Strategies for WebAssembly Learn how to load WebAssembly in real apps - fetch and instantiateStreaming, MIME types, Vite and Webpack imports, lazy loading, caching, Web Workers and fallbacks - with runnable loader examples.
- Plugin Systems with WebAssembly Learn to design a plugin system on WebAssembly - host API as the only imports, allow-lists, LinkError rejection, fuel limits against infinite loops, versioning, data passing and lifecycle - with runnable code.
- The WebAssembly Security Model Understand WebAssembly security - sandboxed linear memory, bounds checks, validation, protected call stack, typed indirect calls, what the sandbox does not protect and host-boundary rules, with runnable demos.
Projects and Practice
- Project: Image Filter in WebAssembly Build an image filter with WebAssembly - RGBA pixel memory, a grayscale and invert module, zero-copy typed-array views, a JavaScript reference, tests, benchmark and the Canvas wiring for the browser.
- Project: Prime Sieve and a Benchmark Harness Build a prime sieve in WebAssembly, verify it against known prime counts, write a reusable benchmark harness with warm-up, median and spread, and learn to interpret the results honestly.
- WebAssembly Practice and Interview Questions Practice WebAssembly with interview questions and answers, coding exercises with runnable solutions, trap-prediction drills, a debugging checklist and a self-assessment, covering memory, imports, WASI and performance.