Docs / Language Manual / If-Else & Loops
Edit

You are currently looking at the v6.0 - v8.2 docs (Reason v3.6 syntax edition). You can find the latest manual page here.

(These docs are equivalent to the old BuckleScript docs before the ReScript rebrand)

If-Else & Loops

ReScript supports if, else, ternary expression (a ? b : c), for and while.

ReScript also supports our famous pattern matching, which will be covered in its own section

If-Else & Ternary

Unlike its JavaScript counterpart, ReScript's if is an expression; they evaluate to their body's content:

Reason (Old Syntax)ML (Older Syntax)JS Output
let message = if (isMorning) {
  "Good morning!"
} else {
  "Hello!"
};

Note: an if-else expression without the final else branch implicitly gives () (aka the unit type). So this:

Reason (Old Syntax)ML (Older Syntax)JS Output
if (showMenu) {
  displayMenu();
};

is basically the same as:

Reason (Old Syntax)ML (Older Syntax)JS Output
if (showMenu) {
  displayMenu();
} else {
  ();
};

Here's another way to look at it. This is clearly wrong:

RE
let result = if (showMenu) { 1 + 2; };

It'll give a type error, saying basically that the implicit else branch has the type unit while the if branch has type int. Intuitively, this makes sense: what would result's value be, if showMenu was false?

We also have ternary sugar, but we encourage you to prefer if-else when possible.

Reason (Old Syntax)ML (Older Syntax)JS Output
let message = isMorning ? "Good morning!" : "Hello!"

if-else and ternary are much less used in ReScript than in other languages; Pattern-matching kills a whole category of code that previously required conditionals.

For Loops

For loops iterate from a starting value up to (and including) the ending value.

Reason (Old Syntax)ML (Older Syntax)JS Output
for (i in startValueInclusive to endValueInclusive) {
  Js.log(i);
};
Reason (Old Syntax)ML (Older Syntax)JS Output
// prints: 1 2 3, one per line
for (x in 1 to 3) {
  Js.log(x);
};

You can make the for loop count in the opposite direction by using downto.

Reason (Old Syntax)ML (Older Syntax)JS Output
for (i in startValueInclusive downto endValueInclusive) {
  Js.log(i);
};
Reason (Old Syntax)ML (Older Syntax)JS Output
// prints: 3 2 1, one per line
for (x in 3 downto 1) {
  Js.log(x);
};

While Loops

While loops execute its body code block while its condition is true.

Reason (Old Syntax)ML (Older Syntax)JS Output
while (testCondition) {
  // body here
};

Tips & Tricks

There's no loop-breaking break keyword (nor early return from functions, for that matter) in ReScript. However, we can break out of a while loop easily through using a mutable binding.

Reason (Old Syntax)ML (Older Syntax)JS Output
let break = ref(false);

while (!break.contents) {
  if (Js.Math.random() > 0.3) {
    break := true;
  } else {
    Js.log("Still running");
  };
};