Skip to main content
Close announcements banner

If-Else expressions

You can create conditional expressions using the keywords if, elseif and else. They evaluate the conditions and return a value when a condition is met. They should follow this syntax:
if (condition1) expression1 elseif (condition2) expression2 else expression3       
You can also specify an expression block containing multiple expressions to evaluate for a condition. Expressions blocks should in curly braces, with expressions separated by , or ;:
if (condition) {expression1, expression2} else expression3         

In this case, expression1 and expression2 are evaluated if the condition is met, and the result of the last expression is returned.

If you want to evaluate an assignment expression, it should be in curly braces, even if there is only one:
if (condition) {identifier1 = expression1} else {identifier2 = expression2}
The following examples are valid conditional expressions:
if (root.test == 1) {
    root.val_1
} else {
    root.val_2
}
$msg = if (data.a < 1) 'lower than 1' elseif (data.a >= 1 && data.a <=2) 'between 1 and 2' else 'greater than 2'
if (item.quantity < 10) { status = "low stock", order.quantity = 50 } else { status = "in stock" }

Did this page help you?

If you find any issues with this page or its content – a typo, a missing step, or a technical error – let us know how we can improve!