What is WebAssembly?

WebAssembly (Wasm) is a binary instruction format for a stack-based virtual machine. That means: a .wasm file holds bytes (numbers from 0 to 255), the bytes…

The booking page of Mist Homestay, Mrs. Sri's small homestay in the mountains, calculates the price of a stay and edits room photos right in the guest's browser. The calculations can be done by WebAssembly (Wasm): a binary instruction format for a stack-based virtual machine (each term is explained below). A .wasm file holds a sequence of bytes that stand for small instructions such as "take this number" or "multiply the top two numbers on the stack". The browser translates those instructions into machine code: instructions that the CPU (your computer's processor) runs directly. Then it runs them inside a sandbox, a closed space that can't touch the rest of your computer.

Think of Wasm as the cook in the homestay kitchen. The cook works fast from recipes written in a standard code that any kitchen understands. The recipe is the .wasm file, and the kitchen is any browser. But the cook never steps out into the lobby. The one who serves the guests (the page, clicks, the network) is still the receptionist, which is JavaScript. The cook only uses what the receptionist passes through the kitchen window: Wasm only uses the data and functions that JavaScript gives it.

The example for this whole track: Mist Homestay

Mrs. Sri has three rooms: the Terrace Room at $35 a night, the Attic Room at $27.50 and the Garden Room at $42.50. Throughout the WebAssembly track we do two things for her:

  1. The first function: the price of a stay, nights × price per night. The Terrace Room for 2 nights = 2 × $35 = $70. The function works on whole numbers, so the code counts in cents: $35 is 3500, and 2 × 3500 = 7000 cents, which is $70.00. We write it in Wasm, and later in Rust and Go, two other programming languages.
  2. The heavy work: room photos. Mrs. Sri uploads 1200 × 800 pixel photos of her rooms. In the browser each photo is turned grey (grayscale), made smaller and blurred. We do it in JavaScript first, then in Wasm, and measure honestly which one is faster.

What Wasm is

What the definition's three terms mean:

This is how the price function, stayPrice(2, 3500), runs, one instruction per row:

Instruction Meaning Stack afterwards
local.get 0 take the first parameter (nights) [2]
local.get 1 take the second parameter (price per night) [2, 3500]
i32.mul take the top two numbers, multiply them [7000]

The number left on the stack, 7,000 (cents, so $70.00), is what the function returns. The number types are fixed and few: i32 and i64 (32- and 64-bit integers), f32 and f64 (floating point, numbers with a fraction). Wasm is rarely written by hand. A .wasm file is usually compiled (translated by a program called a compiler) from another language: C and C++ (through Emscripten), Rust, Go or AssemblyScript.

From code to the browser

The order is always the same:

  1. You write source code.
  2. A compiler makes a .wasm file from that code.
  3. The browser reads the bytes and checks them (validation).
  4. The browser compiles the bytes to machine code for your CPU, then runs them.

From JavaScript, you load the file and call its function like this:

// 47 bytes made by wat2wasm from price.wat (taken apart in the "Wasm Hello World" lesson)
const bytes = new Uint8Array([
  0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x07, 0x01, 0x60,
  0x02, 0x7f, 0x7f, 0x01, 0x7f, 0x03, 0x02, 0x01, 0x00, 0x07, 0x0d, 0x01,
  0x09, 0x73, 0x74, 0x61, 0x79, 0x50, 0x72, 0x69, 0x63, 0x65, 0x00, 0x00,
  0x0a, 0x09, 0x01, 0x07, 0x00, 0x20, 0x00, 0x20, 0x01, 0x6c, 0x0b,
]);

WebAssembly.instantiate(bytes).then(({ instance }) => {
  const { stayPrice } = instance.exports;
  console.log(stayPrice(2, 3500)); // 7000 cents: Terrace Room, 2 nights
});

Why can Wasm be faster for some tasks? Two reasons:

The browser still compiles the Wasm bytes to machine code. V8, the JavaScript engine in Chrome, uses the fast Liftoff compiler first, then the TurboFan compiler for functions called often. But its speed is more predictable, and for heavy computation it comes close to a native program (a program built directly for that CPU). For light work, JavaScript is just as fast. The next lesson measures it.

What Wasm is not

The module below asks JavaScript for one function to show the total. That function, showTotal, is passed in the import object, the second argument:

// imports.wat contains: (import "display" "showTotal" (func $showTotal (param i32)))
WebAssembly.instantiate(importBytes, {
  display: {
    showTotal: (total) => {
      console.log('Total: $' + (total / 100).toFixed(2)); // JS does the writing
    },
  },
}).then(({ instance }) => {
  const { order } = instance.exports;
  order(2, 3500); // Total: $70.00
});

The object's names display and showTotal match the import line in the comment. The order function inside the module calculates 7000 and then calls showTotal. Whatever writes to the console or the page is still JavaScript: a Wasm module can't write "Total: $70.00" onto the page by itself. If you forget to pass the import object, Chrome 147 refuses before any instruction runs, with this error: TypeError: WebAssembly.instantiate(): Imports argument must be present and must be an object.

Where Wasm runs

The same .wasm file runs in all of these places, as long as the imports the module asks for are there. Each engine compiles the file for its own kind of CPU (x86-64, ARM64).

Who uses it

The pattern is the same: the app already has a lot of code in another language, or it needs heavy computation at a steady speed.

📌 The standard: the W3C and versions 1.0, 2.0, 3.0

The W3C is the organisation that sets web standards. A finished standard is called a Recommendation.

The W3C document used in this lesson (the 9 October 2026 version) describes release 3.0 and has the status Candidate Recommendation Draft. The Working Group (the W3C group for Wasm) deliberately keeps it at that status as a living standard (a standard that keeps being updated) and does not move it to Recommendation. Which feature runs in which browser is listed at webassembly.org/features.

⚠️ The usual wrong ideas about Wasm

TL;DR: WebAssembly (Wasm) is a binary instruction format for a stack-based virtual machine. That means: a .wasm file holds bytes (numbers from 0 to 255), the bytes are small instructions, and the instructions work on a stack (a pile of numbers) in a virtual machine (a machine made of software). The browser translates the bytes into machine code (instructions for the CPU) and runs them in a sandbox, a closed and safe space. A .wasm file is usually compiled (translated) from C/C++, Rust or Go. From JavaScript, you load it with WebAssembly.instantiate(bytes) and call the functions it shares (its exports) in instance.exports. Wasm is not a replacement for JavaScript and cannot touch the DOM (the content of the page): it can only call functions that JavaScript gives it as imports. Wasm runs in every modern browser since 2017, and also outside the browser: in Node (JavaScript outside the browser), in the Wasmtime program and at the edge (servers close to the user). Figma, Photoshop on the web and Google Sheets use it. Version 1.0 became a W3C Recommendation (a finished standard) in 2019, 2.0 added SIMD and reference types, and 3.0 (2025) added GC, memory64, exception handling and tail calls. The example for this track: the price of a stay at Mist Homestay (Terrace Room, 2 nights = 7000 cents, $70) and Mrs. Sri's room photos processed in the browser.

⚡ Wasm is a sequence of instruction bytes

Guess first: a .wasm file holds bytes, and those bytes are WebAssembly (Wasm) instructions. Does the browser run a .wasm file as it is?

No. The browser checks the bytes first, then compiles (translates) them to machine code (instructions for your CPU). Only then does it run them, in the same sandbox (a closed space) as JavaScript.

Meaning: WebAssembly is a binary instruction format for a stack-based virtual machine (a machine made of software). It is bytes, and each instruction takes numbers from a stack (a pile of numbers) and puts its result back there. The byte 6c (written in hex) means i32.mul: take the top two numbers on the stack and multiply them. The same .wasm file runs in every modern browser, in Node (JavaScript outside the browser) and at the edge (servers close to the user).

⚡ Load it, then call its exports

Write:

WebAssembly.instantiate(bytes).then(({ instance }) => {
  const { stayPrice } = instance.exports;
  console.log(stayPrice(2, 3500)); // 7000 cents: Terrace Room, 2 nights
});

Meaning: bytes is the content of the .wasm file. WebAssembly.instantiate(bytes) compiles the bytes and creates an instance, a Wasm module (a .wasm file's content) ready to use. It returns a Promise (a value that arrives later). The functions the module shares (its exports) are in instance.exports, and you call them like any JavaScript function. Here stayPrice(2, 3500) calculates the price of 2 nights in a room at 3500 cents ($35) a night.

⚡ Wasm asks for help through imports

Write:

WebAssembly.instantiate(importBytes, {
  display: {
    showTotal: (total) => {
      console.log('Total: $' + (total / 100).toFixed(2)); // JS does the writing
    },
  },
});
Guess first: can a Wasm module write "Total: $70.00" onto the page by itself?

No. Wasm knows nothing of document or fetch. This module only calculates 7000 and then calls showTotal, the JavaScript function in the object above. The writing is still done by JavaScript.

Meaning: A JavaScript function handed to a Wasm module is called an import, and the object in the second argument is the import object. That object must have the same shape as the module's line (import "display" "showTotal" ...): showTotal sits inside display. If you forget to pass the import object, the module is refused with a TypeError before a single instruction runs.

⚡ i32 numbers overflow silently

Meaning: i32 is Wasm's type for 32-bit integers, and the stayPrice function multiplies two i32 numbers. stayPrice(700000, 3500) should be 2,450,000,000, but an i32 only holds up to 2,147,483,647. It overflows: you get -1844967296, with no error.

What you will learn