If Expressions
if
expressions in Aura are declared with the following syntax:
yield
Keyword
It's also important to note that if
expressions are capable of returning a value, which is done with the yield
keyword. So, returning a value looks like this:
When returning a value, the return value of both branches must be the same.
return
Keyword
The programmer may still use the return
keyword inside an if
expression, but it returns the supplied value from the nearest enclosing function scope. This means that, in an example like this:
the "Hello"
value will bypass the s
variable completely and be returned from the enclosing function. However, if the value of the if
expression's condition evaluates to false (which, of course, is impossible when the condition is true
) the value of s
will become World
.
Required else
Branch
When the return value of an if
expression isn't used, the if
expression is not required to have an else
branch. So, this would be fine:
However, if the return value is being stored in a variable, the else
branch is required.