7.3. Operators

MScript, like most languages, provides several operators for working with numbers and for comparing values, among other things:

7.3.1. Arithmetic

The arithmetic operators are used for standard numerical operations like addition, subtraction, multiplication and division. If one of the operands is none, then it will be treated as a 0. For example, 5 + none returns 5, and 5 * none returns 0.

These operators can also be used with arrays: the operators are simply applied to items from both arrays. If one array is shorter than the other, then the final operations will use none as a fallback value. For example, [1, 2, 3] - [5, 4] returns [-4, -2, 3].

Numbers and array can also be combined: in that case, the number is 'reused' while the operation is applied to all items in the array. For example, 4 * [1, 2, 3] returns [4, 8, 12].


a + b

The + operator adds up two numbers. However, if one of the operands is a string then it will perform string concatenation instead.

For example, 4 + 5 returns 9, but 4 + '5' returns '45'.


a - b

The - operator subtracts one number from another number.

For example: 4 - 5 returns -1, and [4, 5] - 1 returns [3, 4].


a * b

The * operator multiplies two numbers.

For example, 4 * 5 returns 20, and 4 * [1, 2, 3] returns [4, 8, 12].


a / b

The / operator divides a number by another number.

For example, 4 / 5 returns 0.8, and [2, 5, 10] / 2 returns [1, 2.5, 5].


a % b

The % operator takes the remainder of a division.

For example, 11 % 4 returns 3, and [4, 5, 6, 7] % 3 returns [1, 2, 0, 1].


7.3.2. Equality

The equality operators can be used with values of any type. They check whether two values are of the same type and whether they represent the same value. Values of different types are never equal.


a == b

The == operator checks if two values are equal.

For example, '5' == 5 returns none (false), but [3, 4] == [3, 4] returns 1 (true).


a != b

The != operator checks whether two values are not equal.

For example, '5' != 5 returns 1 (true), but [3, 4] != [3, 4] returns none (false).


7.3.3. Comparisons

The comparison operators determine whether one number is greater than, equal to or less than another number. As with the arithmetic operators, none is treated as 0. However, unlike the arithmetic operators, the comparison operators cannot be used with arrays.


a > b

The > operator checks if a number is greater than another number.

For example, 5 > 4 returns 1 (true).


a >= b

The >= operator checks if a number is greater than or equal to another number.

For example, 5 >= 5 returns 1 (true).


a < b

The < operator checks if a number is less than another number.

For example, 4 < 5 returns 1 (true).


a <= b

The <= operator checks if a number is less than or equal to another number.

For example, 4 <= 4 returns 1 (true).


7.3.4. Logical

The logical operators compare two boolean (true/false) expressions. These operators have 'short-circuiting' behavior: they will only evaluate the right-hand expression if necessary. This can sometimes be useful for controlling side-effects, such as with a setglobal or a trace function call.


a && b (or a and b)

The && operator (which can also be written as and) returns the second expression if both expressions are true, or none otherwise. It short-circuits: if the first expression is not true, then the second expression will not be evaluated because the result cannot be true anyway.

For example, 'name'.contains('a') && 4 returns 4.


a || b (or a or b)

The || operator (which can also be written as or) returns the first expression if it is true, or otherwise the second expression if that is true, or otherwise none. It short-circuits: if the first expression is true, then the second expression will not be evaluated because the result is already true anyway.

For example, 'name'.contains('a') || 4 returns 1 (the result of the 'name.contains('a') expression).


7.3.5. Unary operators

There are also a few 'unary' operators, which take only a single operand. These are used to negate a numeric value or a boolean expression.


-a

The - operator negates a number. It also works with arrays that contain numbers: it will return an array with all numbers negated.

For example, -max(4, 5) returns -5, and -[4, 5] returns [-4, -5].


!a (or not a)

The ! operator (which can also be written as not) returns 1 (true) if the expression is false, or none (false) otherwise.

For example, !'name'.contains('a') returns none.