What is WebAssembly?

In short
WebAssembly, often shortened to Wasm, is a low-level binary format that browsers can run at near-native speed. Languages such as C, C++, Rust and C# can compile to it, and it runs next to JavaScript inside the same web app.
How it works
WebAssembly is a compact binary format with an assembly-like instruction set. You do not usually write it by hand. Instead, a compiler turns code written in a language like C, C++, Rust or C# into a Wasm module. The browser downloads that module, checks it, and runs it in a sandbox that is separate from the rest of the page.
The key idea is that Wasm is a compilation target. It is a place for compilers to send their output, not a language most people type. Because the instructions are simple and close to what a processor understands, the browser can translate them into machine code quickly. That is where the near-native performance comes from.
WebAssembly is designed to complement JavaScript, not replace it. The two run side by side in the same app. JavaScript handles the user interface, the DOM and the glue logic, while a Wasm module handles heavy numeric work such as image processing, compression or physics.
Talking to JavaScript
JavaScript loads a module through the WebAssembly JavaScript API. The API is exposed on a global object called WebAssembly, which acts as a namespace for everything related to Wasm. Once a module is instantiated, its exported functions appear as ordinary JavaScript functions that you can call.
const response = await fetch("math.wasm");
const bytes = await response.arrayBuffer();
const { instance } = await WebAssembly.instantiate(bytes, {});
const result = instance.exports.add(2, 3);
console.log(result); // 5
This example assumes a module called math.wasm that exports a function named add. The values passed across the boundary are numbers. Wasm works with a small set of value types, so richer data such as strings and objects usually has to be copied into shared memory or converted by the surrounding code. This is one of the main design costs of the model, and it is worth planning for before you start.
If you are new to the language that runs your app's logic, our guide on What is JavaScript? explains the other half of the picture.
When to use it
WebAssembly fits best when you have a well-defined piece of computation that runs for a long time and does not need much interaction with the page. Typical examples include video and audio codecs, games, data parsing, cryptography and scientific calculations. Porting an existing C or Rust library to the browser is another common reason.
It is less useful for simple interface work. Most page logic is short and event-driven, and JavaScript handles it well. Adding a Wasm module in that case adds build steps, download size and debugging effort without a clear gain.
Performance also depends on how often you cross the boundary between JavaScript and Wasm. Many small calls can cost more than one larger call that does the work in one go. Measure before you assume a speedup.
Common mistakes
A frequent mistake is treating WebAssembly as a replacement for JavaScript. The two are meant to cooperate. Trying to move the whole app into Wasm usually makes development harder and rarely pays off.
Another mistake is ignoring the cost of data transfer. If you pass large strings or arrays back and forth on every frame, the overhead can cancel out the speed of the compiled code. Design the interface so that the module receives a batch of work and returns a compact result.
Beginners also often forget that a Wasm module is still a separate file that must be fetched, compiled and instantiated. Large modules delay startup, so keep them small and load them only when needed.
Alternatives
If you only need faster loops or number crunching in JavaScript, you may get enough benefit from typed arrays and careful code before reaching for Wasm. If your work is asynchronous and waits on the network or timers, promises and async functions are usually the better tool. Our guide on How do JavaScript promises work? covers that model.
Outside the browser, other languages offer their own compiled targets and runtimes. Zig, for example, can produce compact native and WebAssembly output from a single codebase. You can read more in What is Zig?.
The official MDN WebAssembly documentation at https://developer.mozilla.org/en-US/docs/WebAssembly is a good place to continue, especially its guides on compiling from different languages and its reference for the JavaScript API.
FAQ
Does WebAssembly replace JavaScript?
No. WebAssembly is designed to run alongside JavaScript. JavaScript usually handles the page and its events, while a Wasm module handles specific heavy computations that you call from JavaScript.
Which languages can compile to WebAssembly?
The official documentation names C, C++, C# and Rust as examples of languages that can serve as compilation targets for Wasm. Other languages have compilers or toolchains that support it too.
Can I write WebAssembly by hand?
You can, because it has a textual representation, but most developers do not. The usual approach is to write code in a higher-level language and let a compiler produce the binary module.
Is WebAssembly only for browsers?
The focus of the MDN documentation is the web, where Wasm runs in the browser. The format itself is a general compilation target, so it can also be used in other environments.
Sources
This page is based on the official documentation below. Read it for full detail.
WebAssembly — official documentation ↗