22title : " Overview"
33metaTitle : " Language Features Overview"
44description : " A quick overview on ReScript's syntax"
5- canonical : " /docs/manual/overview"
5+ canonical : " /docs/manual/v12.0.0/ overview"
66section : " Language Features"
77order : 1
88---
@@ -13,39 +13,53 @@ order: 1
1313
1414### Semicolon
1515
16+ <MarkdownTable >
17+ { `
1618| JavaScript | ReScript |
1719| ---------------------------------- | -------------------- |
1820| Rules enforced by linter/formatter | No semicolon needed! |
21+ ` }< / MarkdownTable >
1922
2023### Comments
2124
25+ <MarkdownTable >
26+ { `
2227| JavaScript | ReScript |
2328| -------------------- | -------------------------------- |
2429| ` // Line comment` | Same |
2530| ` /* Comment */ ` | Same |
2631| ` /** Doc Comment */ ` | ` /** Before Types/Values */ ` |
2732| | ` /*** Standalone Doc Comment */ ` |
33+ ` }</MarkdownTable>
2834
2935### Variable
3036
37+ <MarkdownTable>
38+ { `
3139| JavaScript | ReScript |
3240| ---------------------- - | ------------------------------------ - |
3341| ` const x = 5; ` | ` let x = 5 ` |
3442| ` var x = y; ` | No equivalent (thankfully ) |
3543| ` let x = 5; x = x + 1; ` | ` let x = ref(5); x := x.contents + 1 ` |
44+ ` }</MarkdownTable>
3645
3746### String & Character
3847
48+ <MarkdownTable>
49+ { `
3950| JavaScript | ReScript |
4051| ---------------------------- | -------------------- - |
4152| ` "Hello world!" ` | Same |
4253| ` 'Hello world!' ` | Strings must use ` " ` |
4354| ` "hello " + "world" ` | ` "hello " ++ "world" ` |
4455| ` ` ` hello ${message } ` ` ` | Same |
4556| ` ` sql ` select ${fnName }; ` ` ` | Same |
57+ ` }</MarkdownTable>
4658
4759### Boolean
4860
61+ <MarkdownTable>
62+ { `
4963| JavaScript | ReScript |
5064| ------------------------------------ | ---------------------------------------------- |
5165| ` true ` , ` false ` | Same |
@@ -54,64 +68,83 @@ order: 1
5468| ` a === b ` , ` a !== b ` | Same |
5569| No deep equality (recursive compare ) | ` a == b ` , ` a != b ` |
5670| ` a == b ` | No equality with implicit casting (thankfully ) |
71+ ` }</MarkdownTable>
5772
5873### Number
5974
75+ <MarkdownTable>
76+ { `
6077| JavaScript | ReScript |
6178| ---------- - | ------------ |
6279| ` 3 ` | Same \* |
6380| ` 3.1415 ` | Same |
6481| ` 3 + 4 ` | Same |
6582| ` 3.0 + 4.5 ` | ` 3.0 +. 4.5 ` |
6683| ` 5 % 3 ` | ` mod(5, 3) ` |
84+ ` }</MarkdownTable>
6785
6886\* JS has no distinction between integer and float.
6987
7088### Object/Record
7189
90+ <MarkdownTable>
91+ { `
7292| JavaScript | ReScript |
7393| ------------------ - | -------------------------------------- - |
7494| no types | ` type point = {x: int, mutable y: int} ` |
7595| ` {x: 30, y: 20} ` | Same |
7696| ` point.x ` | Same |
7797| ` point.y = 30; ` | Same |
7898| ` {...point, x: 30} ` | Same |
99+ ` }</MarkdownTable>
79100
80101### Array
81102
103+ <MarkdownTable>
104+ { `
82105| JavaScript | ReScript |
83106| ------------------ | -------------------- - |
84107| ` [1, 2, 3] ` | Same |
85108| ` myArray[1] = 10 ` | Same |
86109| ` [1, "Bob", true] ` | ` (1, "Bob", true) ` \* |
87110
88111\* ReScript does not have heterogenous arrays . Use tuples or [Untagged Variants ](variant #untagged - variants ) instead .
112+ ` }</MarkdownTable>
89113
90114### Null
91115
116+ <MarkdownTable>
117+ { `
92118| JavaScript | ReScript |
93119| ------------------ - | -------- - |
94120| ` null ` , ` undefined ` | ` None ` \* |
121+ ` }</MarkdownTable>
95122
96123\* 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.
97124
98125### Function
99126
127+ <MarkdownTable>
128+ { `
100129| JavaScript | ReScript |
101130| ------------------------------ - | ---------------------------- |
102131| ` arg => retVal ` | Same |
103132| ` function named(arg) {...} ` | ` let named = (arg) => {...} ` |
104133| ` const f = function(arg) {...} ` | ` let f = (arg) => {...} ` |
105134| ` add(4, add(5, 6)) ` | Same |
135+ ` }</MarkdownTable>
106136
107137### Async Function / Await
108138
139+ <MarkdownTable>
140+ { `
109141| JavaScript | ReScript |
110142| -------------------------------------- - | ---------------------------------------------------- - |
111143| ` async (arg) => {...} ` | Same |
112144| ` async function named(arg) {...} ` | ` let named = async (arg) => {...} ` |
113145| ` await somePromise ` | Same |
114146| ` async (arg): Promise<string> => {...} ` | ` async (arg): string => {...} ` (note the return type ) |
147+ ` }</MarkdownTable>
115148
116149### Blocks
117150
@@ -148,49 +181,64 @@ order: 1
148181
149182### If-else
150183
184+ <MarkdownTable>
185+ { `
151186| JavaScript | ReScript |
152187| -------------------- - | -------------------------------------------------------------------------------- - |
153188| ` if (a) {b} else {c} ` | ` if a {b} else {c} ` \* |
154189| ` a ? b : c ` | Same |
155190| ` switch ` | ` switch ` but [super - powered pattern matching ! ](pattern - matching - destructuring .md ) |
191+ ` }</MarkdownTable>
156192
157193\* Our conditionals are always expressions! You can write ` let result = if a {" hello" } else {" bye" }`
158194
159195### Destructuring
160196
197+ <MarkdownTable>
198+ { `
161199| JavaScript | ReScript |
162200| ---------------------------- - | -------------------------- - |
163201| ` const {a, b} = data ` | ` let {a, b} = data ` |
164202| ` const [a, b] = data ` | ` let [a, b] = data ` \* |
165203| ` const {a: aa, b: bb} = data ` | ` let {a: aa, b: bb} = data ` |
204+ ` }</MarkdownTable>
166205
167206\* Gives good compiler warning that ` data ` might not be of length 2.
168207
169208### Loop
170209
210+ <MarkdownTable>
211+ { `
171212| JavaScript | ReScript |
172213| ------------------------------------ - | ---------------------------- |
173214| ` for (let i = 0; i <= 10; i++) {...} ` | ` for i in 0 to 10 {...} ` |
174215| ` for (let i = 10; i >= 0; i--) {...} ` | ` for i in 10 downto 0 {...} ` |
175216| ` while (true) {...} ` | ` while true {...} ` |
217+ ` }</MarkdownTable>
176218
177219### JSX
178220
221+ <MarkdownTable>
222+ { `
179223| JavaScript | ReScript |
180224| ---------------------------------------- - | -------------------------- |
181225| ` <Comp message="hi" onClick={handler} /> ` | Same |
182226| ` <Comp message={message} /> ` | ` <Comp message /> ` \* |
183227| ` <input checked /> ` | ` <input checked=true /> ` |
184228| No children spread | ` <Comp>...children</Comp> ` |
229+ ` }</MarkdownTable>
185230
186231\* Argument punning!
187232
188233### Exception
189234
235+ <MarkdownTable>
236+ { `
190237| JavaScript | ReScript |
191238| ---------------------------------------- - | ------------------------------------------------ |
192239| ` throw new SomeError(...) ` | ` throw(SomeException(...)) ` |
193240| ` try {a} catch (err) {...} finally {...} ` | ` try a catch { \| SomeException(err) => ...} ` \* |
241+ ` }</MarkdownTable>
194242
195243\* No finally.
196244
@@ -231,6 +279,8 @@ The last expression of a block delimited by `{}` implicitly returns (including f
231279
232280## Common Features' JS Output
233281
282+ <MarkdownTable>
283+ { `
234284| Feature | Example | JavaScript Output |
235285| ------------------------------ - | ------------------------------------ | ------------------------------------------ |
236286| String | ` "Hello" ` | ` "Hello" ` |
@@ -253,5 +303,6 @@ The last expression of a block delimited by `{}` implicitly returns (including f
253303| Record | ` type t = {b: int}; let a = {b: 10} ` | ` var a = {b: 10} ` |
254304| Multiline Comment | ` /* Comment here */ ` | Not in output |
255305| Single line Comment | ` // Comment here ` | Not in output |
306+ ` }</MarkdownTable>
256307
257308_Note that this is a cleaned-up comparison table; a few examples' JavaScript output are slightly different in reality._
0 commit comments