## 5.2 Operator OverviewIf
you are a C, C++, or Java programmer, most
of the JavaScript operators should already be familiar to you. Table 5-1 summarizes the operators; you can refer to
this table for reference. Note that most operators are represented by
punctuation characters such as In this 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 understand precedence and associativity, the subsections that follow the table explain these concepts. The operators themselves are documented following that discussion.
## 5.2.1 Number of Operands
Operators
can be categorized based on the number of operands they expect. Most
JavaScript operators, like the ## 5.2.2 Type of Operands When 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 Furthermore, some operators behave differently depending on the type
of the operands. Most notably, the
Notice that the assignment operators, as well as a few other operators, expect their lefthand expressions to be lvalues. lvalue is a historical term that means "an expression that can legally appear on the lefthand side of an assignment expression." In JavaScript, variables, properties of objects, and elements of arrays are lvalues. The ECMAScript specification allows built-in functions to return lvalues but does not define any built-in functions that behave that way. 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 ## 5.2.3 Operator PrecedenceIn Table 5-1, the column labeled "P" specifies the precedence of each operator. Operator precedence controls the order in which operations are performed. Operators with higher numbers in the "P" column are performed before those with lower numbers. Consider the following expression: w = x + y*z; The multiplication operator
Operator precedence can be overridden with the explicit use of parentheses. To force the addition in the previous example to be performed first, we would write: w = (x + y)*z; In practice, if you are at all unsure about the precedence of your operators, the simplest thing is to use parentheses to make the evaluation order explicit. The only rules that are important to know are these: multiplication and division are performed before addition and subtraction, and assignment has very low precedence and is almost always performed last. ## 5.2.4 Operator AssociativityIn Table 5-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, the addition operator has left-to-right associativity, so: w = x + y + z; is the same as: w = ((x + y) + z); On the other hand, the following (almost nonsensical) expressions: x = ~-~y; w = x = y = z; q = a?b:c?d:e?f:g; are equivalent to: x = ~(-(~y)); w = (x = (y = z)); q = a?b:(c?d:(e?f:g)); because the unary, assignment, and ternary conditional operators have right-to-left associativity. |