Home arrow JavaScript arrow Page 5 - The Power of Javascript: Operators concluded

The Power of Javascript: Operators concluded

This is the last article in "The Power of Javascript" series covering operators. In this part, we discuss the logical operators, the operator typof, the void operator, the ternary operator :?, and operators' precedence and Associativity. You may not realize the power and usefulness of operators yet, but when we discuss how you can control your script flow of execution with if/else statements and loop statements, you will realize what operators can do for you, especially the logical operators and the comparison operators.

Author Info:
By: Michael Youssef
Rating: 5 stars5 stars5 stars5 stars5 stars / 16
August 02, 2005
  1. · The Power of Javascript: Operators concluded
  2. · Logical Operators Example
  3. · The Operator typeof
  4. · The Void Operator
  5. · Operator Precedence and Associativity

print this article

The Power of Javascript: Operators concluded - Operator Precedence and Associativity
(Page 5 of 5 )

I know that many of you are asking if the expression 6 + 2 * 4 produces the value 14 or the value 32? Actually it produces the value 14 (not 32) because the interpreter knows in advance which operators must be done first, which operators should be performed next, and so on. The interpreter knows what operators to perform first because each operator has a precedence (order of execution) that comes before or after the other operators. For our example, the multiplication operator has a higher precedence than the addition operator, so the multiplication operation is performed first, which produces the value 8; then the addition operation comes next, so 6 + 8 equal to 14.

The assignment operator (and its variations) has the lowest precedence. This makes sense because what we need in any statement is that the interpreter evaluate and calculate the expression, then assign the value to the variable. There's a way that we can change the precedence behavior and instruct the interpreter to perform the calculations in the order we want; we use the parentheses to delimit the expression that overrides the precedence. Actually, we should do this anyway to make the code more readable. For our example we could produce the value 32 by placing the parentheses around the lower precedence expression (6 + 2) * 4 .

By doing this we instruct the interpreter to add 6 and 2, then multiply by 4, so we get 32. So the parentheses itself is an operator with the highest precedence (with more than two operators; we will discuss this later). That's why the interpreter evaluates the expression that uses the parentheses first.

An Operator has an associativity which defines what direction the operator uses to perform the operation. For example, the addition Operator performs its operation from left to right. When we write something like 4 + 5 + 7, we have used only the addition operator, so the precedence is equal, and the interpreter has to use the associativity of the operator in order to know how it will produce a value out of this expression. Because the addition operator has a left-to-right associativity, it will begin evaluating from left to right, so 4 plus 5 (equals 9) plus 7 equals 16.

We have two types of associativity, left-to-right like the addition operator and right-to-left like the assignment operator, which begins by evaluating the value on the right and assigns it to the variable on the left. An expression like x = z = 10 assigns the value 10 to z, then assigns z to x.

The following table lists all the operators that we have discussed with their precedence and associativity. The highest operator precedence begins with 0 and increases for lower precedence operators. The associativity column specifies whether the operator works from left-to-right or from right-to-left.




0left-to-right() The Parentheses Operator
1right-to-left++ The Increment Operator
1right-to-left-- The Decrement Operator
1right-to-left- the Unary Minus
1right-to-left~ The Bitwise NOT
1right-to-left! The Logical NOT
1right-to-lefttypeof Operator
1right-to-leftvoid Operator
2left-to-right*, /, %
3left-to-right+ Addition and - Subtraction Operators
4left-to-right>> Right Shift With Sign
4left-to-right>>> Right Shift With Zero Fill
4left-to-right<< Bitwise Left Shift
5left-to-right> and >= Operators
5left-to-right< and <= Operators
6left-to-right== The Equal Operator
6left-to-right!= The Not Equal Operator
6left-to-right=== The Strict Equal to Operator
6left-to-right!== The Strict NOT Equal to Operator
7left-to-rightThe Bitwise &
8left-to-rightThe Bitwise XOR ^
9left-to-rightThe Bitwise OR |
10left-to-rightThe Logical AND &&
11left-to-rightThe Logical OR ||
12right-to-left:? The Conditional Operator
13right-to-left= The Assignment Operator
13right-to-leftThe Combined Assignment Operators: *=, /=, %=, +=, -=, <<=, >>=, >>>=, &=, ^=, |=

DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

blog comments powered by Disqus

- Project Nashorn to Make Java, JavaScript Wor...
- JavaScript Virus Attacks Tumblr Blogs
- Google Releases Stable Dart Version, JavaScr...
- Khan Academy Unveils New JavaScript Learning...
- Accessing Nitro? There`s an App for That
- JQuery 2.0 Leaving Older IE Versions Behind
- Fastest JavaScript Engine Might Surprise You
- Microsoft Adjusting Chakra for IE 10
- Brendan Eich: We Don`t Need Google Native Cl...
- An Overview of JavaScript Statements
- An Overview of JavaScript Operators
- Overview of JavaScript Variables
- More of the Top jQuery Social Plugins
- The Top jQuery Social Plugins
- More of the Top jQuery Slider Plugins

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2019 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials