id | keywords | name | summary | category | |||
---|---|---|---|---|---|---|---|
optional-labeled-argument |
|
~arg=? |
This is an `optional labeled argument`. |
languageconstructs |
Labeled arguments, i.e. arguments that are prefixed with ~
, can be suffixed with =?
to denote that they are optional. Thus, they can be
omitted when calling the function.
<CodeTab labels={["ReScript", "JS Output"]}>
let print = (text, ~logLevel=?) => {
switch logLevel {
| Some(#error) => Console.error(text)
| _ => Console.log(text)
}
}
print("An info")
print("An error", ~logLevel=#error)
function print(text, logLevel) {
if (logLevel === "error") {
console.error(text);
} else {
console.log(text);
}
}
print("An info", undefined);
print("An error", "error");