JavaScript
  Home arrow JavaScript arrow Page 2 - 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 - Logical Operators Example


    (Page 2 of 5 )

    Here's an example that explains the logical operators for now (we will use it all the time with if/else statements and loop statements, but for now a simple example is in order). Copy the following code (or write it) to an HTML document and save it.

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
    <html>
      <head>
        <title>Hello World</title>
        <script language="JavaScript" type="text/javascript">
          var x = 5;
          var y = 8;
          var z = 13;

          var result = (y > x && z == (x + y));
          alert("y > x && z == (x + y) is " + result);
          var result = (y < x || z == (x + y));
          alert("y < x || z == (x + y) is " + result);
          var result = 0;
          alert(!result); 
        </script>
      </head>
      <body>
      </body>
    </html>

    Load the document with your browser and you will get the following three message boxes, respectively.

    We have declared x and assigned it the value 5, then y was assigned the value 8, then z received the value 13. The statement var result = (y > x && z == (x + y)); declared and assigned the value true to the variable result by evaluating the expression to the right of the assignment operator. In this expression we have used the comparison operators greater than and equal to to state the relationship between x, y and z. We also combined the logical AND operator (&&) to form a somewhat more complex expression which evaluates to true only if the first operand and the second operand (of the AND Operator) evaluate to true.

    In this example, y is greater than x, so it produces true, and z equals x + y, so it also produces true. Therefore the whole expression evaluates to true. This boolean value is assigned to the variable result and displayed in the message box using the statement alert ("y > x && z == (x + y) is " + result); which concatenates a string value with the value of the variable result.

    There's nothing special about the next statement that uses the logical OR || operator. It just evaluates its left operand, which produces the value false, then it evaluates its right operand which produces the value true. Because the operator || produces the value true if any of its operands evaluate to true, we see true displayed in the message box. The statement var result = 0; assigns the numerical value 0 to the variable result (which means false), then alert (!result); displays a message box that inverts the value of the result variable. The value was false so !false means true. I think that it should be easy by now.

    More JavaScript Articles
    More By Michael Youssef


     

    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 2 hosted by Hostway
    Stay green...Green IT