Language Overview

A concise reference of ReScript's syntax and core language features.

Semicolons

ReScript does not require semicolons. Line breaks are sufficient to separate statements.

Comments

SyntaxPurpose
// Line commentSingle-line comment
/* Block comment */Multi-line comment
/** Doc comment */Documentation comment
/*** Standalone doc comment */Standalone doc comment

Variables

SyntaxDescription
let x = 5Immutable binding
let x = ref(5); x := x.contents + 1Mutable value via ref

Strings

SyntaxDescription
"Hello world!"String literal
"hello " ++ "world"String concatenation
`hello ${message}`String interpolation
sql`select ${col};`Tagged template

Strings must use double quotes (").

Booleans

SyntaxDescription
true, falseBoolean literals
!Logical NOT
||, &&Logical OR, AND
<=, >=, <, >Comparison operators
===, !==Referential (shallow) equality
==, !=Structural (deep) equality

There is no equality with implicit type casting.

Numbers

SyntaxDescription
3Integer literal
3.1415Float literal
3 + 4Addition (works for int and float)
2 / 3 * 4Division and multiplication
2.0 ** 3.0Exponentiation
5 % 3Modulo

Arithmetic operators (+, -, *, /, %, **) work for both int and float.

Records

Records are typed, immutable-by-default data structures with named fields.

SyntaxDescription
type point = {x: int, mutable y: int}Type declaration
{x: 30, y: 20}Record creation
point.xField access
point.y = 30Mutable field update
{...point, x: 30}Immutable update

Arrays

SyntaxDescription
[1, 2, 3]Array literal
myArray[1] = 10Mutable element update

Arrays are homogeneous. For mixed types, use tuples or Untagged Variants.

Tuples

SyntaxDescription
(1, "Bob", true)Tuple literal
let (a, b, c) = tTuple destructuring

Tuples are fixed-length, heterogeneous, and immutable.

Null & Option

ReScript has no null or undefined. The option type represents the possible absence of a value:

SyntaxDescription
NoneNo value
Some("hi")A present value

Functions

SyntaxDescription
arg => retValAnonymous function
let named = (arg) => retValNamed function
add(4, add(5, 6))Function application

Async / Await

SyntaxDescription
async (arg) => {...}Async anonymous function
let named = async (arg) => {...}Async named function
await somePromiseAwait a promise
async (arg): string => {...}Typed async (return type only)

Blocks

The last expression in a {} block is implicitly returned, including in function bodies.

ExampleDescription
let myFun = (x, y) => { let doubleX = x + x let doubleY = y + y doubleX + doubleY }
Function body with implicit return
let result = { let x = 23 let y = 34 x + y }
Block expression bound to a variable

If-Else

SyntaxDescription
if a {b} else {c}Conditional expression
a ? b : cTernary expression
switchPattern matching — see full docs

Conditionals are expressions: let result = if a {"hello"} else {"bye"}

Destructuring

SyntaxDescription
let {a, b} = dataRecord destructuring
let [a, b] = dataArray destructuring *
let {a: aa, b: bb} = dataDestructuring with rename

* The compiler warns if data might not be of length 2.

Loops

SyntaxDescription
for i in 0 to 10 {...}Ascending loop
for i in 10 downto 0 {...}Descending loop
while true {...}While loop

JSX

SyntaxDescription
<Comp message="hi" onClick={handler} />Props
<Comp message />Argument punning
<input checked=true />Explicit boolean props
<Comp>...children</Comp>Children spread

Exceptions

SyntaxDescription
throw(SomeException(...))Raise an exception
try a catch { | SomeException(err) => ...}Catch an exception *

* There is no finally clause.

Compilation Output Reference

A reference showing how common ReScript features compile to JavaScript.

FeatureReScriptJavaScript Output
String"Hello""Hello"
String Interpolation`Hello ${message}`"Hello " + message
Character (discouraged)'x'120 (char code)
Integer23, -2323, -23
Float23.0, -23.023.0, -23.0
Addition23 + 123 + 1
Float Addition23.0 + 1.023.0 + 1.0
Division/Multiply2 / 23 * 12 / 23 * 1
Float Division/Multiply2.0 / 23.0 * 1.02.0 / 23.0 * 1.0
Float Exponentiation2.0 ** 3.02.0 ** 3.0
String Concatenation"Hello " ++ "World""Hello " + "World"
Comparison>, <, >=, <=>, <, >=, <=
Boolean operation!, &&, ||!, &&, ||
Shallow and deep Equality===, =====, ==
List (discouraged)list{1, 2, 3}{hd: 1, tl: {hd: 2, tl: {hd: 3, tl: 0}}}
List Prependlist{a1, a2, ...oldList}{hd: a1, tl: {hd: a2, tl: theRest}}
Array[1, 2, 3][1, 2, 3]
Recordtype t = {b: int}; let a = {b: 10}var a = {b: 10}
Multiline Comment/* Comment here */Not in output
Single line Comment// Comment hereNot in output

Note that this is a cleaned-up reference table; some examples' JavaScript output may differ slightly in practice.