@@ -13,7 +13,7 @@ omitted when calling the function.
13
13
14
14
<CodeTab labels = { [" ReScript" , " JS Output" ]} >
15
15
16
- ``` res
16
+ ``` res example
17
17
let print = (text, ~logLevel=?) => {
18
18
switch logLevel {
19
19
| Some(#error) => Console.error(text)
@@ -41,8 +41,45 @@ print("An error", "error");
41
41
42
42
</CodeTab >
43
43
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
+
44
80
### References
45
81
46
82
* [ Labeled Arguments] ( /docs/manual/latest/function#labeled-arguments )
47
83
* [ Optional Labeled Arguments] ( /docs/manual/latest/function#optional-labeled-arguments )
84
+ * [ Labeled Argument with Default Value] ( /docs/manual/latest/function#optional-with-default-value )
48
85
* [ Function Syntax Cheatsheet] ( /docs/manual/latest/function#tips--tricks )
0 commit comments