Home arrow JavaScript arrow Page 3 - JavaScript Statements

JavaScript Statements

In our last tutorial we covered where to place JavaScript code, how to work with variables, and print stupid things to the screen. In this installment we will be working with Conditional Statements, Switch Statements, and if there is time, Operators. So call up your date and cancel (who am I kidding? You'd rather go on a date than study this) and let's get to work.

Author Info:
By: James Payne
Rating: 5 stars5 stars5 stars5 stars5 stars / 8
October 31, 2007
  1. · JavaScript Statements
  2. · If...Else
  3. · Switch Hitter
  4. · How to Enter Special Characters

print this article

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.



<script type="text/javascript">

var job = “Computers”

switch (job)


case “Computers”:

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


case “Model”:

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


case “Professional Arm Wrestler”:

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



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





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.

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-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials