WebAssembly: What It Is
WebAssembly is a compact binary format that browsers run at near-native speed. You write code in another language and ship it as a small binary file.
Add two numbers in JavaScript
JavaScript adds two numbers with one line.
const total = 2 + 3;
console.log(total);Check that WebAssembly is built in
Every modern browser ships WebAssembly with JavaScript.
console.log(typeof WebAssembly);Example
(async () => {
const B64 = "AGFzbQEAAAABBwFgAn9/AX8DAgEABwcBA2FkZAAACgkBBwAgACABags=";
const bytes = Uint8Array.from(atob(B64), (c) => c.charCodeAt(0));
const { instance } = await WebAssembly.instantiate(bytes, {});
console.log(instance.exports.add(2, 3));
})();Key points
- A binary format stores instructions as bytes, not as readable text.
- Browsers run WebAssembly at near-native speed for heavy work.
- WebAssembly is a target for code written in other languages.
- Module: one packaged unit of WebAssembly code.
- Use WebAssembly beside JavaScript, not as a replacement for it.
- Basic JavaScript is the only skill you need to start this course.
In this example, a small WebAssembly module adds 2 and 3. WebAssembly is a compact binary format that runs beside JavaScript. The loader turns text into bytes, starts the module, and calls its add function. Later lessons build on this first module.
Note
A .wasm file is the compiled output that a browser loads and runs.
Try it Yourself
Edit the code, then press Run.
Runs in your browser β the preview under the editor is the real result. Nothing is sent to a server. The code runner panel (right) does the same with more room.
Exercise
What is WebAssembly?