WebAssembly: Get Started in 10 Minutes
Learn how to compile, run, and interact with your first WebAssembly module
Start TutorialWhat is WebAssembly?
WebAssembly (wasm) is a low-level binary instruction format that's fast, portable across platforms, and integrates well with existing web technologies.
This tutorial will show you how to:
- Create a WebAssembly module from C code
- Load it in the browser
- Call WebAssembly functions from JavaScript
Step-by-Step: Creating Your First WebAssembly Example
Step 1: Write the C Source Code
extern int add_two(int a);
int add_two(int a) {
return a+2;
}
Step 2: Compile to WebAssembly
emcc -O3 -s WASM=1 -s EXPORTED_FUNCTIONS="['_add_two']" -o add2.wasm add_two.c
Step 3: Load and Execute in Web
fetch('add2.wasm')
.then(response => response.arrayBuffer())
.then(bytes => WebAssembly.instantiate(bytes))
.then(results => {
const { add_two } = results.instance.exports;
console.log(add_two(5)); // Output: 7
});
What Can You Do Next?
- Explore optimizing performance-critical code in C/C++ and calling it from JavaScript
- Experiment with WebAssembly in real web applications
- Try using popular libraries like Emscripten or WASI
- Read the full WebAssembly Specification