Learn WAT with Real Code Examples
Updated Nov 25, 2025
Code Sample Descriptions
1
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.
2
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.
3
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.
4
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.
5
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.
6
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.
7
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.
8
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.
9
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.
10
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.