Page 345 - Webapplication11_C11_Flipbook
P. 345

4.16 ASSOCIATIVITY AND OPERATOR PRECEDENCE
                 When an expression contains a large number of operators and operands and the sequence of evaluations is uncertain,
                 operator precedence and associative rules come into play.

                 Operator Precedence

                 Operator precedence refers to how the operator interacts with its operand. It determines which operation takes
                 precedence over the others. The hierarchy of precedence operators is established, with the operator with the highest
                 precedence standing at the top. For example, because the multiplication operator takes precedence over the addition
                 operator, the multiplication operation is executed before the addition operation in an expression.

                 Operator Associativity
                 The order in which an operator evaluates its operand, i.e. from left to right or right to left. When two operators have
                 equal precedence in an expression, the association is generally left to right.

                               Precedence           Type                 Operators             Associativity

                                    1        Postfix            () [] ->. ++--                 Left to right
                                    2        Unary              +-!~++--(type)* & sizeof       Right to left
                                    3        Multiplicative     */%                            Left to right

                                    4        Additive           +-                             Left to right
                                    5        Shift              <<, >>                         Left to right
                                    6        Relational         <<=>>=                         Left to right
                                    7        Equality           ==!=                           Left to right

                                    8        Bitwise AND        &                              Left to right
                                    9        Bitwise XOR        ^                              Left to right
                                    10       Bitwise OR         |                              Left to right
                                    11       Logical AND        &&                             Left to right

                                    12       Logical OR         II                             Left to right
                                    13       Conditional        ?:                             Right to left
                                    14       Assignment         =+=-=*=/=%=>>=<<=&=^=|=        Right to left

                                    15       Comma              ,                              Left to right

                 Let us take some examples of operator precedence.

                 Ð ÐIf var a = 8, then the output of the following expression will be:
                 Ð a += ++a + a-- % --a
                 Ð Output:
                 Ð a = 8 + 9 + 9 % 7
                 Ð a = 17 + 2
                 Ð a = 19

                 Ð ÐIf var a = 7, then what is the value of a after evaluating the expression given below:
                 Ð a+=a++ + ++a - --a + a--
                 Ð Output:
                 Ð a += a++ + ++a - --a + a--
                 Ð a = 7 + (a++ + ++a - --a + a--)

                                                                                                  JavaScript Part 1  343
   340   341   342   343   344   345   346   347   348   349   350