Skip to content
Tutorials Center
My learning

Topics

Lesson 1 of 38 ยท 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.

WebAssembly, usually shortened to Wasm, is a compact binary instruction format that runs in a sandboxed virtual machine. Browsers ship that virtual machine, so a web page can download a .wasm file and run it at close to native speed. Node.js, Deno, Bun and many other runtimes ship the same machine, so the same file can also run on a server or on a command line.

The name is slightly misleading. WebAssembly is not assembly language for one specific processor, and it is not only for the web. Think of it as a portable, safe, fast compilation target: you write code in a language such as C, C++, Rust, Go or Zig, a compiler turns it into a .wasm module, and the module runs the same way everywhere. This course teaches the format itself, so by the end you will understand exactly what those compilers produce.

The problem Wasm solves

For two decades JavaScript was the only language a browser could run. JavaScript is flexible and has become fast, but it is a poor target for code that was written for other languages: a physics engine, a video codec, an image library, a database. Translating such code into JavaScript was slow to start, unpredictable to optimize and large to download.

WebAssembly fixes this with four properties:

  • Fast to load. A .wasm file is already compiled to a compact binary form. The engine only decodes and validates it, which is much quicker than parsing source text.
  • Fast to run. The instructions are low level and typed, so the engine can generate machine code without guessing types at run time.
  • Safe. Wasm code runs in a sandbox. It cannot touch the page, the file system or the network unless the host hands it a function to do so.
  • Portable. The same bytes run on every browser, operating system and CPU architecture that has a Wasm engine.

One binary, many places

Source code C, C++, Rust, Go, Zig and more.
Compiler Emits a .wasm module.
Any host Browser, Node.js, edge, desktop.

Compile once, run anywhere there is an engine.

1 / 3

WebAssembly next to JavaScript

The two are partners. JavaScript still owns the page: it handles the DOM, events, networking and most application logic. WebAssembly handles the hot loops and the code you already have in another language. A typical app loads a .wasm module, calls a handful of exported functions from JavaScript, and lets those functions crunch numbers.

Question JavaScript WebAssembly
Delivered as Text source Compact binary
Types Dynamic Four number types, fixed at compile time
Memory Garbage collected objects One big resizable array of bytes (plus optional GC types)
Talks to the DOM Directly Only through JavaScript
Typical author You, by hand A compiler, from another language
flowchart LR
    A["Source: C, Rust, Zig..."] --> B["Compiler"]
    B --> C["module.wasm (binary)"]
    C --> D["Engine: decode, validate, compile"]
    D --> E["Instance: running code and memory"]
    J["JavaScript"] <--> E

The binary format has a text twin called the WebAssembly Text Format (.wat). It is a readable way to write the same program. You will learn it in chapter 3. Every runnable example in this course is a real .wasm module built from bytes, so you can see what an engine really receives.

What Wasm is good at

Use WebAssembly when the work is computation-heavy and self-contained:

  • Image, audio and video processing (filters, codecs, resizing).
  • Games and physics engines, and 3D software ported from desktop.
  • Cryptography, compression and hashing.
  • Scientific and data code: matrix math, parsing, simulation.
  • Running whole programs from other languages: SQLite, Python, PDF renderers, code editors.
  • Safe plug-in systems, because a module can only do what you give it.

What it is not good at

Do not rewrite a normal CRUD web page in Wasm. Wasm cannot read or change the DOM on its own, so UI code is easier in JavaScript. Crossing the boundary between the two worlds has a cost, so a design that calls a tiny Wasm function a million times can be slower than plain JavaScript. And the first version of the format has no built-in strings or objects: text and structures are encoded in memory by hand, or by the compiler. Later lessons show how.

Is WebAssembly available here?

Every modern browser and every current Node.js has a global WebAssembly object. Run this to see it, and to check the smallest module that is valid: eight bytes consisting of the magic number \0asm and the version 1.

Node.js Does this engine support Wasm?

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

The empty module has no functions, so it exports nothing. It still proves the pipeline works: the engine decoded it, validated it and created an instance.

A module that does something

Here is a slightly larger module, again written byte by byte. It exports one function, answer, that takes no arguments and returns the 32-bit integer 42. The comments explain each group of bytes; you will meet every piece properly in the next lessons.

Node.js A function that returns 42

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

That is a complete program, 39 bytes long. Calling instance.exports.answer() from JavaScript jumped into compiled Wasm code and came back with a number.

The big ideas to keep

  • A module is the compiled, stateless code (like a class definition). An instance is a module plus its memory and imports (like an object).
  • Wasm only has numbers at its core. Everything else is built on top.
  • The host (JavaScript, Node.js, a native runtime) decides what the module can see.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which statement about WebAssembly is true?

    Wasm is a binary instruction format run by an engine. It works in all major browsers and many other runtimes, and it is usually produced by compilers.
  2. 2. Can a Wasm module change the page's DOM directly?

    A module has no ambient access to anything. The host imports functions into it, and those functions can touch the DOM.
  3. 3. Which workload is the best fit for WebAssembly?

    Compute-heavy, self-contained loops benefit most. DOM chores stay in JavaScript.
  4. 4. What are the first eight bytes of every valid module?

    Every module starts with the bytes for 0, a, s, m followed by the little-endian version number 1.

Summary

  • WebAssembly is a portable, sandboxed, binary compilation target supported by browsers and server runtimes.
  • It runs beside JavaScript, not instead of it: JavaScript owns the page, Wasm does heavy computation.
  • A module is stateless code; an instance adds memory and imports.
  • Great for codecs, games, cryptography and ported libraries; poor for plain DOM work.
  • The eight-byte header \0asm + version 1 is the smallest valid module.

Next: How WebAssembly Runs