Operators Precedence

written by: George Freedrich; article published: year 2008, month 10;

In: Root » Computers and technology » AJAX and JavaScript » Operators Precedence

  Share  
|
  NL  |  FR  |  ES  |  PT  |  IT  |  DE  |  NO  |  JP  |  CN  |  KR  |  RU  |  AE


The order in which expressions are evaluated based on their operators is known as precedence. Multiplication and division occur before addition and subtraction, so any operands that are to be multiplied or divided occur before ones that are added and subtracted. Precedence can be reordered by placing expressions within parentheses. The innermost parentheses are evaluated first and work outward. So, if you want two numbers added before multiplication, place them in parentheses. The following two script excerpts show the difference results from different precedence order:

var alpha = 3 * 4 + 7 //alpha's value is 19 — 12 + 7 
var beta = 3 * (4 + 7) // beta's value is 33 — 3 * 11 

When all of the operators have the same precedence, the evaluations occur from left to right. The table below a precedence chart, with the lowest ranks being executed before the higher ones.

Operators Precedence

Rank

Operators

1

. [] ()

2

++ -- - (negation) ~ ! delete new typeof void

3

* / %

4

+ - (subtraction, addition, or concatenation)

5

<< >> >>> (bitwise shifts)

6

< > <= >=

7

= = != = = = != =

8

& (bitwise)

Rank

Operators

9

^ (bitwise)

10

| (bitwise)

11

&&

12

||

13

?: (ternary)

14

= All compound assignments (such as +=, /=, and &=)

15

,

Share

Disclaimer

1) E-articles is not responsible for the information contained by this article as well for any and all copyright infringements by authors and writers. E-articles is a free information resource. If you suspect this article for any copyright infringement, please read the terms of service and contact us or use the "Report this article" button on this page to investigate the problem.
2) E-articles is not responsible for inaccuracies, falsehoods, or any other types of misinformation this article may contain and will not be liable for any loss or damage suffered by a user through the user's reliance on the information gained here.

link to this article