Skip to content

ReasonML Cheatsheet

Reason syntax for OCaml — familiar JS-like syntax with OCaml's type safety.

01

Getting Started

Hello World

ReasonML compiles to JavaScript via BuckleScript.

reasonml
Js.log("Hello, World!");

Let

let bindings end with semicolons.

reasonml
let x = 10;
let y = 20;
Js.log(x + y);

Functions

Arrow functions like JS/ES6.

reasonml
let add = (a, b) => a + b;
Js.log(add(3, 4));

Types

Record types with JS-like syntax.

reasonml
type person = {name: string, age: int};
let p = {name: "Alice", age: 30};
Js.log(p.name);
02

Features

Variant

Variants with switch for pattern matching.

reasonml
type result =
  | Ok(int)
  | Err(string);

let r = Ok(42);
switch (r) {
| Ok(v) => Js.log(v)
| Err(e) => Js.log(e)
};

Option

Option type for nullable values.

reasonml
let safeDiv = (a, b) =>
  if (b === 0) { None } else { Some(a / b) };

Pipe

Pipe-first operator |>.

reasonml
[1, 2, 3, 4, 5]
|> List.filter(x => x mod 2 === 0)
|> List.map(x => x * 2)
|> Js.log;

Was this helpful?