Using Expressions

An expression is a compound value that evaluates to determine a result. We have already encountered one example of an expression called an assignment statement. A simple expression uses an equals sign to assign a value to a variable. Expressions can contain operators that modify or join the values of some variables to come up with a final result. In this section, we look at several different forms of expressions.

Topics:

Mathematical Expressions

The kinds of expressions that most people are familiar with are arithmetic expressions. Here is an example of an expression that adds two numbers together and assigns the result to a variable:

myVar = 2 + 2; 
aa.print(myVar); 

This script displays this output:

4

In this example the “+” operator joins two numbers. Here is another example:

myVar = 1 
myVar = myVar + 2; 
aa.print(myVar); 
firstVar = 7; 
secondVar = 5; 
myVar = firstVar + secondVar; 
aa.print(myVar); 

This script displays this output:

3 
12 

On the second line of this script, we add two to the current value of myVar and put the resulting new value back into and put the resulting new value back into myVar. On the seventh line of this script we add two variables to come up with a result that we place in myVar. There are operators for addition, subtraction, multiplication, division, and many more. We do not cover every arithmetic operator here, but here are six operators arithmetic operators you can use:

Table 1. Mathematical Operators
Symbol Description Example
+
Addition
myVar = 2 + 2;
myVar now contains 4.
-
Subtraction
myVar = 4 – 2;
myVar now contains 2.
*
Multiplication
myVar = 2 * 3;
myVar now contains 6.
/
Division. Be careful not to divide by zero or you get an error.
myVar = 6 / 2;
myVar now contains 3.
%
Modulus. The “remainder” operator. Tells you the left over amount, after division.
myVar = 5% 3;
myVar now contains 2.
- 
(negation)
Negation. The “unary” operator. Takes whatever value you put immediately to the right of it, and reverses its sign.
someVar = 3;
myVar = -someVar;
myVar now contains –3.
Note:

Civic Platform does not support decimal precision in mathematical expressions. If you want to ensure the precision of your mathematical results, consider using mathematical functions instead. For more information, see Precision of Numbers.

You can also use more than one operator at a time in an expression. For example:

firstVar = 7; 
secondVar = 5; 
myVar = firstVar - 2 + secondVar + 7; 
aa.print(myVar); 

This script displays this output:

17

When using a single line that contains several operators it is important to remember that, just as in your grade school mathematics classes, some operators have a higher precedence than others do. For example:

myVar = 2 + 6 / 3;

myVar now contains 4.

The result of the expression in this example was four because the division operator has a higher precedence than the addition operator does. All operators, including the non-arithmetic operators, have a certain level of precedence. When two operators in the same expression have the same level of precedence, Civic Platform evaluates them in left to right order. For example:

myVar = 6 * 3 / 3;

myVar now contains 6.

You can use parentheses to change the order in which to evaluate an expression:

MyVar = (2 + 6) / 4;

MyVar now contains 2.

In general, Civic Platform evaluates everything inside a set of parentheses before anything outside the parentheses.

String Expressions

String expressions are quite simple. There is only one operator that works on strings. We use the “+” operator for addition and to concatenate two strings together end to end to form a new string. Here is an example:

firstVar = “Hello”; 
secondVar = “World.”; 
thirdVar = firstVar + secondVar; 
aa.print(thirdVar); 

This script displays this output:

Hello World.

You can concatenate more than two strings together:

myVar = “Hello” + “to the “ + “world.”; 
aa.print(myVar); 

This script displays this output:

Hello to the world.

Boolean Expressions

Boolean expressions always evaluate to either true or false. Boolean Operators shows the most common Boolean operators for boolean expressions.

Table 2. Boolean Operators
Symbol Description Example
&&
And
myVar = true && false
;myVar now contains false.
||
Or
myVar = true || 2
;myVar now contains true.
!
Not
someVar = true
;
myVar = !someVar
;myVar now contains false.

First let us examine the “and” operator “&&”. This operator is true when both of its operands are true, and false the rest of the time. The word “operands” refers to the thing that the operator is operating on. So for example, an inspector has an inspection scheduled for today and the inspector has called in sick. The facts that the inspector has an inspection scheduled and that he has called in sick can be operands of the && operator. Both are true, so the operation returns a result of true. And Operator Results shows possible results of the “&&” operator.

Table 3. And Operator Results
Example myVar Contains
myVar = true && true;
true
myVar = true && false;
false
myVar = false && true;
false
myVar = false && false;
false

You use the “&&” operator most often when you want to find out if two or more things are true at the same time.

Next we examine the “or” operator “||”. You use the “||” operator most often when you want to find out if at least one of two or more things is true. The result is true as long as at least one of the operands is true. You use vertical bar character (also called a pipe), that is on the same key as the backslash character, to type this operator. Press shift backslash to type this character. Or Operator Results shows a set of examples for the “||” operator.

Table 4. Or Operator Results
Example myVar Contains
myVar = true || true;
true
myVar = true || false;
true
myVar = false || true;
true
myVar = false || false;
false

Finally, we examine the “not” operator “!”. The “!” is a unary operator that operates on only one operand. Like the unary minus sign, the not operator reverses the state of the value to which it applies (Not Operator Results).

Table 5. Not Operator Results
Example myVar Contains
myVar = !true;
false
myVar = !false;
true

You can use multiple Boolean operators in a row, and use parentheses to change the order of precedence of Boolean operators, just like arithmetic operators. However, there is an additional aspect of Boolean operators not shared by other operators called “short-circuit evaluation.” Here are two examples of this:

myVar = false && ???;

myVar contains false no matter what is on the right hand side of the “&&”.

myVar = true || ???;

myVar contains true no matter what is on the right hand side of the “||”.

Short-circuit evaluation means that if Civic Platform can determine from the first part of an expression whether the whole expression is going to be true or false it does not bother to evaluate the rest of the expression.

Relational Operators

Relational operators return either true or false. However, unlike Boolean operators they can take different kinds of operands like numbers and strings. The relational operators are ==, !=, <, >, <=, and >=.

The “equals” operator “==” tells us if two values are the same. See Relational Operators.

Table 6. Relational Operators
Example myVar Contains
myVar = (true == true);
true
myVar = (true == false);
false
myVar = (false == true);
false
myVar = (false == false);
true
myVar = (1 == 2);
false
myVar = (2 == 2)
true
myVar = (“Hello” == “World”);
false
myVar = (“Hello” == “Hello”);
true

The “==” operator uses two equals signs to avoid confusion with the assignment operator. You usually use the “==” operator to find out if two things are the same. You can compare any two values using this operator. You can find out if a variable has a special value like null as in this example:

myVar = (someVar == null);

The “!= operator is the opposite of the “==” operator. See Relational Operators.

Table 7. Relational Operators
Example myVar Contains
myVar = (true != true);
false
myVar = (true != false);
true
myVar = (false != true);
true
myVar = (false != false);
false
myVar = (1 != 2);
true
myVar = (2 != 2);
false
myVar = (“Hello” != “World”);
true
myVar = (“Hello” != “Hello”);
true

The <, >, <=, and >= operators are useful when comparing two numbers. See Relational Operators.

Table 8. Relational Operators
Example myVar Contains
myVar = 1 < 2;
true
myVar = 2 < 1;
false
myVar = 1 > 2;
false
myVar = 2 > 1;
true
myVar = 1 <= 2;
true
myVar = 2 <= 1;
false
myVar = 1 <= 1;
true
myVar = 1 >= 2;
false
myVar = 2 >= 1;
true
myVar = 1 >= 1;
true

Special Operators

We only cover one special operator here. We have already seen this operator when we first investigated creating an array. The “new” operator creates a new object. For example:

myVar = new Array();

myVar now contains an Array object.

Arrays are really just a special kind of object. We go over more about arrays in a later section. For now, we should take note that the keyword “new” is a special kind of operator that creates a new copy of an object type. In this example the object type was “Array”. We learn more about creating objects in the section Objects, Methods, and Properties. There are many special operators that we have not covered. For more information on special operators, consult a JavaScript reference.

Operator Precedence

The operators have the precedence in the order shown.

= 
|| 
&& 
== != 
< > <= >= 
+ - 
* / % 
! - (unary minus) 
new