The if...else
conditional expression returns one of two values based on a condition. It looks like this:
if (condition) {
value1
} else {
value2
}
There are some rules that will be enforced:
- The
else
branch must be present, if it's missing you will get a syntax error. This ensures you handle all possibilities. - The
condition
must evaluate to typeBool
. - The values of both branches must evaluate to the same type
{% hint style="info" %} Currently there is no shorthand for a conditional expression. {% endhint %}
Multiple if..else
statements can be written in sequence:
if (number > 5) {
true
} else if (number > 2 {
true
} else {
false
}