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)
Overview
Note: the comparison is against our Reason syntax, not our ML syntax.
| JavaScript | Us |
|---|
| Rules enforced by linter/formatter | No semicolon needed! |
| JavaScript | Us |
|---|
/* Comment */ | Same |
// Line comment | Same |
| JavaScript | Us |
|---|
const x = 5; | let x = 5 |
var x = y; | No equivalent (thankfully) |
let x = 5; x = x + 1; | let x = ref(5); x := x.contents + 1 |
| JavaScript | Us |
| ------------------------ | --------------------- | ---------------- | --- |
| "Hello world!" | Same |
| 'Hello world!' | Strings must use " |
| "hello " + "world" | "hello " ++ "world" |
| `hello ${message}` | \{j | Hello $(message) | j} |
| JavaScript | Us |
|---|
true, false | Same |
!true | Same |
||, &&, <=, >=, <, > | Same |
a === b, a !== b | Same |
| No deep equality (recursive compare) | a == b, a != b |
a == b | No equality with implicit casting (thankfully) |
| JavaScript | Us |
|---|
3 | Same * |
3.1415 | Same |
3 + 4 | Same |
3.0 + 4.5 | 3.0 +. 4.5 |
5 % 3 | 5 mod 3 |
* JS has no distinction between integer and float.
| JavaScript | Us |
|---|
| no types | type point = {x: int, mutable y: int} |
{x: 30, y: 20} | Same |
point.x | Same |
point.y = 30; | Same |
{...point, x: 30} | Same |
| JavaScript | Us |
|---|
[1, 2, 3] | [|1, 2, 3|] |
myArray[1] = 10 | Same |
[1, "Bob", true] | (1, "Bob", true) * |
* Heterogenous arrays in JS are disallowed for us. Use tuple instead.
| JavaScript | Us |
|---|
null, undefined | None * |
* Again, only a spiritual equivalent; we don't have nulls, nor null bugs! But we do have an option type for when you actually need nullability.
| JavaScript | Us |
|---|
arg => retVal | Same |
function named(arg) {...} | let named = (arg) => {...} |
const f = function(arg) {...} | let f = (arg) => {...} |
add(4, add(5, 6)) | Same |
| JavaScript | US |
|---|
const myFun = (x, y) => {
const doubleX = x + x;
const doubleY = y + y;
return doubleX + doubleY;
};
| let myFun = (x, y) => {
let doubleX = x + x
let doubleY = y + y
doubleX + doubleY
}
|
* Our conditionals are always expressions! You can write let result = if (a) {"hello"} else {"bye"}
| JavaScript | Us |
|---|
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.
| JavaScript | Us |
|---|
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) {...} |
| JavaScript | Us |
|---|
<Comp message="hi" onClick={handler} /> | Same |
<Comp message=message /> | <Comp message /> * |
<input checked /> | <input checked=true /> |
| No children spread | <Comp>...children</Comp> |
* Argument punning!
| JavaScript | Us |
|---|
throw new SomeError(...) | raise(SomeError(...)) |
try {a} catch (Err) {...} finally {...} | try a catch { | Err => ...} * |
* No finally.
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 | US |
|---|
let result = (function() {
const x = 23;
const y = 34;
return x + y;
})();
| let result = {
let x = 23
let y = 34
x + y
}
|
| Feature | Example | JavaScript Output |
| ------------------------------- | ------------------------------------ | ------------------------------------------ | --- | -------------------- |
| String | "Hello" | "Hello" |
| String Interpolation | \{j | Hello $(message) | j} | "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) | [1, 2, 3] | {hd: 1, tl: {hd: 2, tl: {hd: 3, tl: 0}}} |
| List Prepend | [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 |