JavaScript
  Home arrow JavaScript arrow Page 5 - The Power of Javascript: Operators conclud...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVASCRIPT

The Power of Javascript: Operators concluded
By: Michael Youssef
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 6
    2005-08-02

    Table of Contents:
  • The Power of Javascript: Operators concluded
  • Logical Operators Example
  • The Operator typeof
  • The Void Operator
  • Operator Precedence and Associativity

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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.

    Precedence

    Associativity

    Operator

    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.

     

    JAVASCRIPT ARTICLES

    - Using Click Interceptions with a Database-Dr...
    - Using JavaScript Click Interceptions in an I...
    - Using Click Interceptions with JavaScript
    - QuickSort in Action
    - Quicksort
    - Using Mod_Security to Protect Your Server
    - Detecting and Countering Server Intrusions
    - Securing Your Web Server
    - Building a Secure Web Server
    - Protecting the Server
    - Book Review: Learning the Yahoo! User Interf...
    - Dynamically Generate a Selection List in a R...
    - Intergrate DWR into Your Java Web Application
    - Detect Browser Compatibility with the Reques...
    - Using the EXT JS Date Picker Widget






    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
    Stay green...Green IT