Learn Swift-wasm - 10 Code Examples & CST Typing Practice Test
SwiftWasm is a toolchain and ecosystem that compiles Swift code into WebAssembly, allowing developers to run Swift applications directly in the browser, WASI runtimes, serverless platforms, and edge environments. It brings Swift’s safety, performance, and modern language features to WebAssembly-based execution environments.
Learn SWIFT-WASM with Real Code Examples
Updated Nov 25, 2025
Code Sample Descriptions
Simple SwiftWasm Program
# swift/demo/main.swift
import JavaScriptKit
let console = JSObject.global.console
console.log!("Hello, SwiftWasm!")
A basic Swift program compiled to WebAssembly that prints 'Hello, SwiftWasm!' to the JavaScript console.
SwiftWasm Add Two Numbers
# swift/demo/add.swift
import JavaScriptKit
let console = JSObject.global.console
func add(a: Int, b: Int) {
console.log!("Sum: \(a + b)")
}
add(a: 3, b: 5)
Adds two integers and prints the sum to console.
SwiftWasm Multiply Two Numbers
# swift/demo/multiply.swift
import JavaScriptKit
let console = JSObject.global.console
func multiply(a: Int, b: Int) {
console.log!("Product: \(a * b)")
}
multiply(a: 4, b: 6)
Multiplies two integers and prints the result.
SwiftWasm Fibonacci
# swift/demo/fibonacci.swift
import JavaScriptKit
let console = JSObject.global.console
func fib(_ n: Int) -> Int {
if n <= 1 { return n }
return fib(n - 1) + fib(n - 2)
}
console.log!("Fibonacci(6): \(fib(6))")
Calculates nth Fibonacci number recursively and prints it.
SwiftWasm Factorial
# swift/demo/factorial.swift
import JavaScriptKit
let console = JSObject.global.console
func factorial(_ n: Int) -> Int {
if n <= 1 { return 1 }
return n * factorial(n - 1)
}
console.log!("Factorial(5): \(factorial(5))")
Calculates factorial recursively and prints result.
SwiftWasm Even Check
# swift/demo/even.swift
import JavaScriptKit
let console = JSObject.global.console
func isEven(_ n: Int) {
console.log!(n % 2 == 0 ? "Even" : "Odd")
}
isEven(7)
isEven(10)
Checks if a number is even and prints the result.
SwiftWasm Maximum of Two Numbers
# swift/demo/max.swift
import JavaScriptKit
let console = JSObject.global.console
func max(_ a: Int, _ b: Int) {
console.log!("Max: \(a > b ? a : b)")
}
max(7, 12)
Prints the maximum of two numbers.
SwiftWasm Minimum of Two Numbers
# swift/demo/min.swift
import JavaScriptKit
let console = JSObject.global.console
func min(_ a: Int, _ b: Int) {
console.log!("Min: \(a < b ? a : b)")
}
min(7, 12)
Prints the minimum of two numbers.
SwiftWasm Toggle Boolean
# swift/demo/toggle.swift
import JavaScriptKit
let console = JSObject.global.console
var state = false
func toggle() {
state.toggle()
console.log!(state ? "ON" : "OFF")
}
toggle()
toggle()
Toggles a boolean value and prints ON/OFF.
SwiftWasm Print Array
# swift/demo/array.swift
import JavaScriptKit
let console = JSObject.global.console
let arr = [1, 2, 3, 4, 5]
for num in arr {
console.log!(num)
}
Prints elements of an array to console.
Frequently Asked Questions about Swift-wasm
What is Swift-wasm?
SwiftWasm is a toolchain and ecosystem that compiles Swift code into WebAssembly, allowing developers to run Swift applications directly in the browser, WASI runtimes, serverless platforms, and edge environments. It brings Swift’s safety, performance, and modern language features to WebAssembly-based execution environments.
What are the primary use cases for Swift-wasm?
Running Swift code in the browser. Building rich client-side apps using Swift + DOM APIs. WASI-based CLI tools written in Swift. Edge computing microservices in Swift. Full-stack Swift (backend + Wasm frontend)
What are the strengths of Swift-wasm?
Modern, expressive Swift language for web/edge apps. Strong type safety and memory safety. Great interoperability with JavaScript in browser. Fast and efficient Wasm binaries via LLVM. Unified full-stack Swift experience
What are the limitations of Swift-wasm?
Not all Swift stdlib APIs are available. Apple frameworks (UIKit, Foundation Networking, SwiftUI) are not supported in Wasm. Binary sizes can be larger than TinyGo/AssemblyScript. Limited multithreading due to Wasm constraints. Smaller ecosystem compared to JS/Rust for Wasm
How can I practice Swift-wasm typing speed?
CodeSpeedTest offers 10+ real Swift-wasm code examples for typing practice. You can measure your WPM, track accuracy, and improve your coding speed with guided exercises.