In the numeric expression , 5 + 4 “ 2 , three numbers are combined. The operators are the + and “ signs. The operands for the + sign are 5 and 4 . After that part of the expression is evaluated to 9 , the expression becomes 9 “ 2 . After evaluating the complete expression, the result is 7 . Since the plus and minus operators each manipulate two operands, they are called a binary operators. If there is only one operand, the operator is called a unary operator. If there are three operands, it is called a ternary operator. We'll see examples of these operators later in the chapter.
An assignment statement evaluates the expression on the right-hand side of the equal sign and assigns the result to the variable on the left-hand side of the equal sign. The equal sign is the assignment operator.
var total = 5 + 4; var friend = "Tony";
5.1.2 Precedence and Associativity
Associativity refers to the order in which an operator evaluates its operands: left to right in no specified order, or right to left. When all of the operators in an expression are of equal precedence, normally the association is left to right; in the expression 5 + 4 + 3 , the evaluation is from left to right. In Example 5.1, how is the expression evaluated? Is addition, multiplication, or division done first? And in what order, right to left or left to right?
In Table 5.1 the operators on the same line are of equal precedence. The rows are in order of highest to lowest precedence.
Table 5.1. Precedence and associativity.
Figure 5.1. Output from Example 5.1.