This is the latest docs version
Quick Links
  • -Overview
  • -Language Features
  • -JS Interop
  • -Build System
Documentation
Language Manual
Reference for all language features
ReScript & React
First class bindings for ReactJS
GenType
Seamless TypeScript integration
Reanalyze
Dead Code & Termination analysis
Exploration
Packages
Explore third party libraries and bindings
Syntax Lookup
Discover all syntax constructs
APIPlaygroundBlogCommunity
  • Playground
  • Blog
  • Twitter
  • GitHub
  • Forum
Language Manual
Overview
  • Introduction
  • Installation
  • Migrate to v11
  • Editor Plugins
  • Try
Language Features
  • Overview
  • Let Binding
  • Type
  • Primitive Types
  • Tuple
  • Record
  • Object
  • Variant
  • Polymorphic Variant
  • Null, Undefined and Option
  • Array & List
  • Function
  • If-Else & Loops
    • If-Else & Ternary
    • For Loops
    • While Loops
  • Pipe
  • Pattern Matching / Destructuring
  • Mutation
  • JSX
  • Exception
  • Lazy Value
  • Promises
  • Async / Await
  • Tagged templates
  • Module
  • Import & Export
  • Attribute (Decorator)
  • Reserved Keywords
  • Equality and Comparison
Advanced Features
  • Extensible Variant
  • Scoped Polymorphic Types
JavaScript Interop
  • Interop Cheatsheet
  • Embed Raw JavaScript
  • Shared Data Types
  • External (Bind to Any JS Library)
  • Bind to JS Object
  • Bind to JS Function
  • Import from / Export to JS
  • Bind to Global JS Values
  • JSON
  • Inlining Constants
  • Use Illegal Identifier Names
  • Generate Converters & Helpers
  • Browser Support & Polyfills
  • Libraries & Publishing
  • TypeScript
Build System
  • Overview
  • Configuration
  • Configuration Schema
  • External Stdlib
  • Pinned Dependencies
  • Interop with JS Build Systems
  • Performance
  • Warning Numbers
Guides
  • Converting from JS
Extra
  • Newcomer Examples
  • Project Structure
  • FAQ
Docs / Language Manual / If-Else & Loops
Edit

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:

ReScriptJS 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:

ReScriptJS Output
if showMenu {
  displayMenu()
}

is basically the same as:

ReScriptJS Output
if showMenu {
  displayMenu()
} else {
  ()
}

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

RES
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.

ReScriptJS 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.

ReScriptJS Output
for i in startValueInclusive to endValueInclusive {
  Console.log(i)
}
ReScriptJS Output
// prints: 1 2 3, one per line
for x in 1 to 3 {
  Console.log(x)
}

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

ReScriptJS Output
for i in startValueInclusive downto endValueInclusive {
  Console.log(i)
}
ReScriptJS Output
// prints: 3 2 1, one per line
for x in 3 downto 1 {
  Console.log(x)
}

While Loops

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

ReScriptJS 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.

ReScriptJS Output
let break = ref(false)

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

© 2024 The ReScript Project

Software and assets distribution powered by KeyCDN.

About
  • Community
  • ReScript Association
Find us on