Skip to main content
Z3’s JavaScript bindings provide both high-level (Z3Py-like) and low-level (C-like) APIs. This guide covers the high-level API, which is recommended for most use cases.

Quick Start

Create a simple Z3 program:
example.js

High-Level API Overview

The high-level API provides a clean, object-oriented interface similar to Z3Py.

Initialization

Always start by initializing Z3:
The init() function is async because it loads the WebAssembly module.

Creating Variables

Building Expressions

Solving Constraints

Common Examples

Example 1: Solving Linear Equations

Solve the system:
  • x + y = 10
  • x - y = 2

Example 2: Boolean Logic

Solve: (p ∨ q) ∧ (¬p ∨ ¬q)

Example 3: Bit-Vector Arithmetic

Example 4: Using Simplifiers

From src/api/js/examples/high-level/simplifier-example.ts:

Example 5: Parsing SMT-LIB2

Context Isolation

Z3 contexts are isolated from each other. TypeScript types enforce this at compile time:

Working with Multiple Contexts

Use templated functions to propagate types:

Asynchronous Operations

Long-running operations are async and run in separate threads. Only one can run at a time.
These operations are async:
  • solver.check()
  • solver.check(assumptions)
  • solver.consequences()
  • tactic.apply()

Using SMT-LIB2 Format

You can parse and work with SMT-LIB2 strings:

TypeScript Support

The bindings include full TypeScript definitions:

Memory Management

Z3’s WebAssembly module manages memory automatically. Objects are garbage collected when no longer referenced.
For long-running applications, consider periodically creating fresh contexts to avoid memory buildup.

Error Handling

Next Steps

API Reference

Complete API documentation

Examples

More examples and tutorials

Low-Level API

C-like API documentation

Source Examples

Example code on GitHub