4.2 Operator OverviewIf you are a C, C++, or Java programmer, then the JavaScript operators will almost all be already familiar to you. Table 4.1 summarizes the operators, and you can refer to this table for reference. In the table, the column labeled P gives the operator precedence, and the column labeled A gives the operator associativity, which can be L (left-to-right) or R (right-to-left). If you do not already know C, C++, or Java, the sections that follow the table explain how to interpret the table and explain what each of the operators does.
Number of OperandsIn general, there are three types of operators. Most JavaScript operators, like the + operator that we saw in the previous section, are binary operators that combine two expressions into a single, more complex expression. That is, they operate on two operands. JavaScript also supports a number of unary operators, which convert a single expression into a single more complex expression. The - operator in the expression -3 is a unary operator which performs the operation of negation on the operand 3. Finally, JavaScript supports one ternary operator, ?:, which combines the value of three expressions into a single expression. Type of OperandsWhen constructing JavaScript expressions, you must pay attention to the data types that are being passed to operators, and to the data types that are returned. Different operators expect their operands' expressions to evaluate to values of a certain data type. For example, it is not possible to multiply strings, so the expression "a" * "b" is not legal in JavaScript. Note, however, that JavaScript tries to convert expressions to the appropriate type whenever possible, so the expression "3" * "5" is legal. Its value is the number 15, not the string "15". Furthermore, some operators behave differently depending on the type of the operands. Most notably, the + operator adds numeric operands but concatenates string operands. And if passed one string and one number, it converts the number to a string and concatenates the two resulting strings. For example, '1' + 0 yields the string '10'. Finally, note that operators do not always return the same type as their operands. The comparison operators (less than, equal to, greater than, etc.) take operands of various types, but when comparison expressions are evaluated, they always return a Boolean result that indicates whether the comparison is true or not. For example, the expression a < 3 returns true if the value of variable a is in fact less than 3. As we'll see, the Boolean values returned by comparison operators are used in if statements, while loops, and for loops--JavaScript statements that control the execution of a program based on the results of evaluating expressions that contain comparison operators. Operator PrecedenceIn Table 4.1 the column labeled P specifies the precedence of each operator. Operator precedence controls the order in which operations are performed. Operators with a lower number in the P column are performed before those with a higher number. Somewhat confusingly, we say that operators that are performed first (with a lower P number) have higher precedence. Consider the following expression:
w = x + y*z;
w = (x + y)*z; Operator AssociativityIn Table 4.1 the column labeled A specifies the associativity of the operator. A value of L specifies left-to-right associativity, and a value of R specifies right-to-left associativity. The associativity of an operator specifies the order in which operations of the same precedence are performed. Left-to-right associativity means that operations are performed from left to right. For example:
w = x + y + z;
w = ((x + y) + z);
x = ~-~y; w = x = y = z; q = a?b:c?d:e?f:g;
x = ~(-(~y)); w = (x = (y = z)); q = a?b:(c?d:(e?f:g)); |
|