Skip to content

Commit a393b13

Browse files
committed
Continue writing JavaScript cheatsheet
1 parent 4d5fcef commit a393b13

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

cheatsheets/gleam-for-javascript-users.md

+57
Original file line numberDiff line numberDiff line change
@@ -345,3 +345,60 @@ are fully type checked.
345345
| Concatenate | `+` | `<>` | In Gleam both values must be **strings** |
346346
| Pipe | | `|>` | Gleam's pipe can pipe into anonymous functions |
347347

348+
### Notes on operators
349+
350+
- JavaScript operators are short-circuiting as in Gleam.
351+
- Gleam's `/` operator always returns an integer.
352+
- Chains and pipes:
353+
- In JavaScript chaining is usually done by constructing class methods that
354+
return an object: `foo.bar(1).quux(2)` means `bar(1)` is called as a method
355+
of `foo` and then `quux()` is called as a method of the return value
356+
(object) of the `bar(1)` call.
357+
- In contrast in Gleam piping, no objects are being returned but mere data is
358+
pushed from left to right, much like in unix tooling.
359+
360+
## Constants
361+
362+
#### JavaScript
363+
364+
In JavaScript constants are just regular variables using the `const` keyword.
365+
366+
```javascript
367+
const THE_ANSWER = 42;
368+
369+
function main() {
370+
const justANormalVariable = "can also use the const keyword";
371+
return THE_ANSWER;
372+
}
373+
```
374+
375+
#### Gleam
376+
377+
In Gleam constants are also created using the `const` keyword. The difference to
378+
JavaScript is that Gleam constants can only live at the top-level of a module
379+
(not inside a function), and variables defined using `let` can only live inside
380+
functions (not at the top-level of a module).
381+
382+
```gleam
383+
const the_answer = 42
384+
385+
pub fn main() {
386+
the_answer
387+
}
388+
```
389+
390+
Additionally, Gleam constants can be referenced from other modules.
391+
392+
```gleam
393+
// in file other_module.gleam
394+
pub const the_answer: Int = 42
395+
```
396+
397+
```gleam
398+
import other_module
399+
400+
fn main() {
401+
other_module.the_answer
402+
}
403+
```
404+

0 commit comments

Comments
 (0)