JavaScript
  Home arrow JavaScript arrow Page 2 - JavaScript Statements
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  
Dedicated Servers  
Moblin 
JMSL Numerical Library 
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

JavaScript Statements
By: James Payne
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 5
    2007-10-31

    Table of Contents:
  • JavaScript Statements
  • If...Else
  • Switch Hitter
  • How to Enter Special Characters

  • 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


    JavaScript Statements - If...Else


    (Page 2 of 4 )

    Now let's say we wanted something to happen if the value of our variable job had been different. In that case we would use an If...Else statement, which basically says: If this is true, do that, else do something else. I bet you want to see that in code. Then put on your glasses and take a gander.


    <html>

    <body>


    <script type="text/javascript">


    var job = “Computers”


    if (job == “Computers”)

    {

    document.write("<b>You must be fat.</b>")

    }

    else

    {

    document.write("<b>You must not be fat</b>")

    }

    </script>

    </body>

    </html>

    Look at that masterful coding. You can only hope to be a coding wizard like me some day. The above code extends our previous sample. If the value of jobs is Computers, it will still print the line: You must be fat. However, if the value of jobs is anything else, it will print: You must not be fat.

    If...Else If...Else

    Let's say we wanted to insult people that worked in several different fields. We can do that as well; all we need is our friend the If...Else If....Else Statement.


    <html>

    <body>


    <script type="text/javascript">

    var job = “Computers”


    if (job == “Computers”)

    {

    document.write("<b>You must be fat.</b>")

    }

    else if (job == “Model” || job == “Supermodel”)

    {

    document.write("<b>You must be dumb. It's good that yer pretty.</b>")

    }

    else

    {

    document.write("<b>That sounds boring.</b>")

    }

    </script>

    </body>

    </html>

    Once again we have modified our original If statement. If the value of job is equal to “Computers” (which it is), it still prints the line: You must be fat. This time however, we also ask if the value of job is Model OR Supermodel. It isn't, but if it was either value, then the following would be printed to your screen: You must be dumb. It's good that yer pretty. And lastly, if the value was anything else, this would have printed: That sounds boring.

    Just to clarify, you can add as many else if statements as you want, and insult all the jobs you can imagine. It's a great way to kill an hour or two on a Friday.

    More JavaScript Articles
    More By James Payne


       · Thanks for dropping by the read my article on Javascript. In this exciting episode...
     

    JAVASCRIPT ARTICLES

    - 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
    - Ajax Hack for Entering Information Without R...
    - EXT JS 2.1 Overview
    - Using the Style Object for Zebra Tables with...
    - Binary Searching
    - An Improved Approach to Building Zebra Tables
    - Assigning Background Colors Dynamically to Z...
    - Building Zebra Tables with CSS and JavaScript
    - JavaScript: Array Objects
    - A Closer Look at Smart Markers with Yahoo! M...
    - Using Polylines and Smart Markers with Yahoo...







    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway