Learn Wat - 10 Code Examples & CST Typing Practice Test
WAT (WebAssembly Text Format) is the human-readable, assembly-like syntax used to represent WebAssembly binaries. It allows developers to write, inspect, debug, and understand Wasm modules using a clear, text-based format before compiling to .wasm.
Learn WAT with Real Code Examples
Updated Nov 25, 2025
Code Sample Descriptions
Simple WAT Module
# wat/demo/add.wat
(module
(func $add (param $a i32) (param $b i32) (result i32)
local.get $a
local.get $b
i32.add)
(export "add" (func $add))
)
A basic WebAssembly Text Format (WAT) module that adds two numbers.
WAT Multiply Module
# wat/demo/multiply.wat
(module
(func $multiply (param $a i32) (param $b i32) (result i32)
local.get $a
local.get $b
i32.mul)
(export "multiply" (func $multiply))
)
A WAT module that multiplies two numbers.
WAT Subtract Module
# wat/demo/subtract.wat
(module
(func $subtract (param $a i32) (param $b i32) (result i32)
local.get $a
local.get $b
i32.sub)
(export "subtract" (func $subtract))
)
A WAT module that subtracts two numbers.
WAT Factorial Module
# wat/demo/factorial.wat
(module
(func $factorial (param $n i32) (result i32)
(local $result i32)
(local.get $n)
i32.const 1
i32.le_s
if (result i32)
then i32.const 1
else
local.get $n
local.get $n
i32.const 1
i32.sub
call $factorial
i32.mul
end)
(export "factorial" (func $factorial))
)
A WAT module that calculates factorial recursively.
WAT Fibonacci Module
# wat/demo/fibonacci.wat
(module
(func $fib (param $n i32) (result i32)
local.get $n
i32.const 1
i32.le_s
if (result i32)
then local.get $n
else
local.get $n
i32.const 1
i32.sub
call $fib
local.get $n
i32.const 2
i32.sub
call $fib
i32.add
end)
(export "fib" (func $fib))
)
A WAT module that calculates Fibonacci numbers recursively.
WAT Max Module
# wat/demo/max.wat
(module
(func $max (param $a i32) (param $b i32) (result i32)
local.get $a
local.get $b
i32.gt_s
if (result i32)
then local.get $a
else local.get $b
end)
(export "max" (func $max))
)
A WAT module that returns the maximum of two numbers.
WAT Min Module
# wat/demo/min.wat
(module
(func $min (param $a i32) (param $b i32) (result i32)
local.get $a
local.get $b
i32.lt_s
if (result i32)
then local.get $a
else local.get $b
end)
(export "min" (func $min))
)
A WAT module that returns the minimum of two numbers.
WAT Even Check Module
# wat/demo/even.wat
(module
(func $isEven (param $n i32) (result i32)
local.get $n
i32.const 2
i32.rem_s
i32.eqz)
(export "isEven" (func $isEven))
)
A WAT module that checks if a number is even.
WAT Odd Check Module
# wat/demo/odd.wat
(module
(func $isOdd (param $n i32) (result i32)
local.get $n
i32.const 2
i32.rem_s
i32.const 0
i32.ne)
(export "isOdd" (func $isOdd))
)
A WAT module that checks if a number is odd.
WAT Toggle Module
# wat/demo/toggle.wat
(module
(global $state (mut i32) (i32.const 0))
(func $toggle (result i32)
global.get $state
i32.const 1
i32.xor
global.set $state
global.get $state)
(export "toggle" (func $toggle))
)
A WAT module that toggles a boolean value between 0 and 1.
Frequently Asked Questions about Wat
What is Wat?
WAT (WebAssembly Text Format) is the human-readable, assembly-like syntax used to represent WebAssembly binaries. It allows developers to write, inspect, debug, and understand Wasm modules using a clear, text-based format before compiling to .wasm.
What are the primary use cases for Wat?
Learning WebAssembly internals. Debugging or inspecting Wasm modules. Creating tiny hand-crafted Wasm binaries. Reverse engineering WebAssembly. Testing Wasm instructions or host bindings
What are the strengths of Wat?
Extremely lightweight and minimal. Perfect for learning WebAssembly internals. Direct control over Wasm structure and instructions. Readable and easy to experiment with. Useful for debugging compiler output
What are the limitations of Wat?
Not suitable for large-scale application development. No high-level abstractions (loops, structs, variables). Verbose for anything beyond small modules. Hard to maintain manually. Manual memory management required
How can I practice Wat typing speed?
CodeSpeedTest offers 10+ real Wat code examples for typing practice. You can measure your WPM, track accuracy, and improve your coding speed with guided exercises.