Home arrow JavaScript arrow Page 2 - JavaScript Statements
JAVASCRIPT

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
TABLE OF CONTENTS:
  1. · JavaScript Statements
  2. · If...Else
  3. · Switch Hitter
  4. · How to Enter Special Characters

print this article
SEARCH DEVARTICLES

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.


blog comments powered by Disqus
JAVASCRIPT ARTICLES

- 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 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials