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.
|
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 |
, |
Our website is not responsible for the information contained by this article. Webworldarticles.com is a free articles resource thus practically any visitor can submit an article. However if you notice any copyrighted material, please contact us and we will remove the article(s) in discussion right away.
This article was sent to us by:
George Freedrich at
10232008
1. The ''with'' Statement
All articles in this directory are property of their respective authors. Additionally, read our Privacy Policy
© 2010 WebWorldarticles.com - All Rights Reserved. Partners: Gunblade Saga