You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* Our conditionals are always expressions! You can write let result = if a {"hello"} else {"bye"}
Destructuring
JavaScript
ReScript
const {a, b} = data
let {a, b} = data
const [a, b] = data
let [a, b] = data *
const {a: aa, b: bb} = data
let {a: aa, b: bb} = data
* Gives good compiler warning that data might not be of length 2.
Loop
JavaScript
ReScript
for (let i = 0; i <= 10; i++) {...}
for i in 0 to 10 {...}
for (let i = 10; i >= 0; i--) {...}
for i in 10 downto 0 {...}
while (true) {...}
while true {...}
JSX
JavaScript
ReScript
<Comp message="hi" onClick={handler} />
Same
<Comp message={message} />
<Comp message /> *
<input checked />
<input checked=true />
No children spread
<Comp>...children</Comp>
* Argument punning!
Exception
JavaScript
ReScript
throw new SomeError(...)
raise(SomeError(...))
try {a} catch (err) {...} finally {...}
try a catch { | SomeError(err) => ...} *
* No finally.
Blocks
The last expression of a block delimited by {} implicitly returns (including function body). In JavaScript, this can only be simulated via an immediately-invoked function expression (since function bodies have their own local scope).
JavaScript
ReScript
``` let result = (function() { const x = 23; const y = 34; return x + y; })(); ```
``` let result = { let x = 23 let y = 34 x + y } ```
Common Features' JS Output
Feature
Example
JavaScript Output
String
"Hello"
"Hello"
String Interpolation
`Hello ${message}`
"Hello " + message
Character (disrecommended)
'x'
120 (char code)
Integer
23, -23
23, -23
Float
23.0, -23.0
23.0, -23.0
Integer Addition
23 + 1
23 + 1
Float Addition
23.0 +. 1.0
23.0 + 1.0
Integer Division/Multiplication
2 / 23 * 1
2 / 23 * 1
Float Division/Multiplication
2.0 /. 23.0 *. 1.0
2.0 / 23.0 * 1.0
Float Exponentiation
2.0 ** 3.0
Math.pow(2.0, 3.0)
String Concatenation
"Hello " ++ "World"
"Hello " + "World"
Comparison
>, <, >=, <=
>, <, >=, <=
Boolean operation
!, &&, ||
!, &&, ||
Shallow and deep Equality
===, ==
===, ==
List (disrecommended)
list{1, 2, 3}
{hd: 1, tl: {hd: 2, tl: {hd: 3, tl: 0}}}
List Prepend
list{a1, a2, ...oldList}
{hd: a1, tl: {hd: a2, tl: theRest}}
Array
[1, 2, 3]
[1, 2, 3]
Record
type t = {b: int}; let a = {b: 10}
var a = {b: 10}
Multiline Comment
/* Comment here */
Not in output
Single line Comment
// Comment here
Not in output
Note that this is a cleaned-up comparison table; a few examples' JavaScript output are slightly different in reality.