Skip to content

Commit 82640a9

Browse files
Syntax Lookup: Default labeled args
1 parent a974e65 commit 82640a9

File tree

1 file changed

+38
-1
lines changed

1 file changed

+38
-1
lines changed

misc_docs/syntax/language_optional_labeled_argument.mdx

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ omitted when calling the function.
1313

1414
<CodeTab labels={["ReScript", "JS Output"]}>
1515

16-
```res
16+
```res example
1717
let print = (text, ~logLevel=?) => {
1818
switch logLevel {
1919
| Some(#error) => Console.error(text)
@@ -41,8 +41,45 @@ print("An error", "error");
4141

4242
</CodeTab>
4343

44+
Optional labeled arguments can also hold a default value.
45+
46+
<CodeTab labels={["ReScript", "JS Output"]}>
47+
48+
```res example
49+
let print = (text, ~logLevel=#info) => {
50+
switch logLevel {
51+
| #error => Console.error(text)
52+
| #warn => Console.warn(text)
53+
| #info => Console.log(text)
54+
}
55+
}
56+
57+
print("An info")
58+
print("A warning", ~logLevel=#warn)
59+
```
60+
61+
```js
62+
function print(text, logLevelOpt) {
63+
var logLevel = logLevelOpt !== undefined ? logLevelOpt : "info";
64+
if (logLevel === "warn") {
65+
console.warn(text);
66+
} else if (logLevel === "error") {
67+
console.error(text);
68+
} else {
69+
console.log(text);
70+
}
71+
}
72+
73+
print("An info", undefined);
74+
75+
print("A warning", "warn");
76+
```
77+
78+
</CodeTab>
79+
4480
### References
4581

4682
* [Labeled Arguments](/docs/manual/latest/function#labeled-arguments)
4783
* [Optional Labeled Arguments](/docs/manual/latest/function#optional-labeled-arguments)
84+
* [Labeled Argument with Default Value](/docs/manual/latest/function#optional-with-default-value)
4885
* [Function Syntax Cheatsheet](/docs/manual/latest/function#tips--tricks)

0 commit comments

Comments
 (0)