Here is a table summarizing the assignment operators, most of which can be secondarily classified as members of other operator categories:
Value: | Equivalent: | Meaning: |
= | a = b | Simple assignment to an LValue |
+= | a = a + b | Add and assign to an LValue |
-= | a = a - b | Subtract and assign to an LValue |
*= | a = a * b | Multiply and assign to an LValue |
/= | a = a / b | Divide and assign to an LValue |
%= | a = a % b | Remainder and assign to an LValue |
&= | a = a & b | Bitwise AND and assign to an LValue |
|= | a = a | b | Bitwise inclusive OR and assign to an LValue |
^= | a = a ^ b | Bitwise exclusive XOR and assign to an LValue |
<<= | a = a << b | Bitwise shift left and assign to an LValue |
>>= | a = a >> b | Bitwise shift right and assign to an LValue |
>>>= | a = a >>> b | Bitwise shift right (unsigned) and assign to an LValue |
++ | a = a + 1 | Increment LValue |
-- | a = 1 - 1 | Decrement LValue |
Assignment operators include the simple assignment as well as the compound OP= form where OP is one of the shift, bitwise, multiplicative or additive operators.
Note that these operators are destructive. That is one of the source operands is overwritten by the result. In most implementations, it is unlikely that this compound assignment executes more efficiently than the long form version, which reads more clearly and is less prone to accidental damage.
Although the ECMA standard describes the algorithms to be used for evaluating operators, there is no guarantee that the operands themselves will be evaluated in any particular order. The right one might appear to be the sensible choice for being evaluated first, since the final value of the left one is dependant on it. However, this is implementation dependant and certain interpreter designs are based around a recursive descent model which may partially evaluate the left operand before pausing momentarily while the right is evaluated.
The operand on the left of the operator must be a modifiable LValue. You cannot use these compound operators with a pair of constant literal values although the right hand operand can be a constant. The left one must be capable of being assigned to.
It is a general assumption that the left value will be a single variable. However it could be an array element or object property in which case the resolution of the identifier may cause some side effects that are undesirable and may interact with the right hand operand value.
The compound operators are considered to be a single token and the characters that comprise them may not be separated by white space. The operator should be separated from the operands by white space however. Some implementations may forgive the lack of white space, but this could lead to ambiguities during interpretation. Such errors may be difficult to diagnose.
ECMA 262 edition 2 - section - 11.13
ECMA 262 edition 2 - section - 12.2
ECMA 262 edition 3 - section - 11.13
ECMA 262 edition 3 - section - 12.2
Wrox Instant JavaScript - page - 20
Prev | Home | Next |
Assignment expression | Up | Associative array indexing |
JavaScript Programmer's Reference, Cliff Wootton Wrox Press (www.wrox.com) Join the Wrox JavaScript forum at p2p.wrox.com Please report problems to support@wrox.com © 2001 Wrox Press. All Rights Reserved. Terms and conditions. |