Comments for expressions

by Ricardo “Rudxain” Fernández Serrata

Version 1 (August 4, 2021)

Download (58 downloads)

By using logical operators, you can add comments to expressions. No extra blocks required.

The parentheses I used are unnecessary in these specific cases, but parentheses are recommended to avoid surprises related to operator precedence.

To make sure that the interpreter optimizes code, so that the comments don't affect performance, use the ternary op in this way:
truthy constant ? expression : "comment"

Or this way:
falsy constant ? "comment" : expression

If your comment is not a falsy value, you can also use "&&":
"truthy-value comment" && expression

"||" requires evaluation of the left side expression, which can negatively affect performance when the left side is a variable, because variables can have any value, the interpreter can't optimize comments that use this operator. The variable on the left must be truthy, otherwise the comment is returned.

If you don't know what "truthy" or "falsy" means in AM, read the documentation:

Or maybe even the MDN documentation about Javascript truthiness:

[] and {} are falsy. [null] and {"": null} are truthy because they aren't empty (even if the only value that fills them is null)

4.3 average rating from 3 reviews

5 stars
4 stars
3 stars
2 stars
1 star

Rate and review within the app in the Community section.