An expression is a statement that results into a value. Most expressions are used with operators. For example, x = 2 is an expression that uses the assignment operator (=) to assign the literal 2 to the variable x.
Operators have precedence, which changes the order the code is executed.
Since * and / have higher precedence than + and -, the above expressions result in 10 and 8. Using parentheses to create a grouped expression changes the precedence of the operators.
The grouped expressions are evaluated first, so they result in 18 and 2.
Operators in HSL can be either unary, binary, or ternary.
Unary operators require a single operand before or after the operator.
When an unary operator is placed before the operand, it's called a prefix unary operator. Otherwise, if an unary operator is placed after the operand, it's called a postfix unary operator. Examples of expressions with unary operators are ++a, !value, typeof text, and b--.
Binary operators require two operands: one before the operator, and one after the operator.
The above is called an infix binary operator because the operator is between the two operands. Examples of expressions with binary operators are 5 + 3, a > b, and 10 /= 2.
HSL only has one kind of ternary operator:
This operator evaluates condition, and if it's a truthy value, evaluates the expressionIfTruthy expression and skips the expressionIfFalsey expression. Otherwise, if condition evaluates to a falsey value, the operator skips the expressionIfTruthy expression and evaluates the expressionIfFalsey expression.
This can be used as an alternative for if-else statements.
The code above is equivalent to the code below:
A constant expression is one that can be evaluated at compile time. They can be assigned to a var or a const variable, but const variables require a constant expression.
The compiler is able to determine whether a variable can be const if it's a constant expression that does not assigned to a global. For example:
This compiles correctly, but there will be compiler warnings:
Changing the above variables to const makes the warning disappear, but now a different warning appears:
Since the compiler can now determine quantity * cost is a constant expression, it started to warn about it. Changing total to be const makes all warnings disappear:
Constant expressions can be folded, which means that the compiler can generate the result of the expression, rather than generate code that causes the expression to be computed at runtime. This makes your code take less time to execute. See the following script, and then the bytecode that the compiler generated for both functions:
Instead of generating code that executes 2 + 2 and "Hello" + " world", the compiler evaluated those expressions and generated the result. Compare with bytecode generated for the same script, but the const variables are declared with var instead:
If a binary operation consists of numbers, and either value is a decimal, the result is always a decimal.
Numbers can be casted to integers with Number.AsInteger, and to decimals with Number.AsDecimal.
Strings can be concatenated with numbers.
typeof can be used to retrieve the type of a variable. It returns a string.