JavaScript
  Home arrow JavaScript arrow Page 3 - 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 - Switch Hitter


    (Page 3 of 4 )

    As you can see from the above example, using the If...Else If...Else statement can lead to some lengthy code. And we all know you are lazy. I mean, how many times does your mother tell you to pick up your socks for crying out loud? And that pizza you ordered last week is so old it's now a calzone.

    A simpler way to work with blocks of If...Else statements is to use the Switch Statement, which analyses a variable, and then checks through the values of each case. If it finds a match, it will execute that block of code. If not it moves on to the next case.


    <html>

    <body>

    <script type="text/javascript">

    var job = “Computers”


    switch (job)

    {

    case “Computers”:

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

    break

    case “Model”:

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

    break

    case “Professional Arm Wrestler”:

    document.write("<b>Over the Top should have won an Oscar.</b>")

    break

    default:

    document.write("<b>I don't really care what you do.</b>")

    }

    </script>

    </body>

    </html>

    In this code, the variable job is given the value “Computers”. Then the value of job is checked against each case. If there is a match, it will execute the block of code. As you can see above, the first case is a match, so it would print:

      You must be fat.

    If the value was “Model”, it would have skipped the first case and executed the Model case, in which case, it would have printed: You must be dumb. It's good that yer pretty. And again, if the value was Professional Arm Wrestler, it would have skipped the first two cases and executed on the third case, resulting in the print out: Over the Top should have won an Oscar.

    And finally, if none of the case's criteria are met, the default case is executed. In this case it would have printed: I don't really care what you do.

    As you can see, the above saves us some coding and is easier to read if we ever need to refer to it again.

    You will also notice the Break function. This prevents the code from automatically executing the next case.

    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 4 hosted by Hostway