JavaScript: Using the Switch Statement

Posted by ActiveEnnovations

     As your programming skills grow, you move from small projects to bigger projects. As your projects get bigger, so does the amount of code they use. Even if the project does require a good amount of code, you will begin to look for shortcuts or faster ways to achieve a goal in the program. One such shortcut is the “Switch” statement. Using this statement in your program will essentially remove the need for a mess of “If…Else” statements. The ideal place for a “Switch” statement is in a program that offers the user options of what to do.

for example:

<script type= “text/javascript”>
var x= 2
switch(x)
{
  case 1:
   document.write(“One”);
   break;

  case 2:
   document.write(“Two”);
   break;

  case 3:
   document.write(“Three”);
   break;

  default:
   document.write(“Not One, Two, or Three”);
}
</script>

The example may be too simple, but it effectively shows the point. The value of variable ‘x’ is 2, so when the “Switch” statement is reached, it will jump to “case 2” and execute the code. The break statements after each case ensure that the code does not keep running through to the next cases.

Posted on: 8/17/2010 at 4:34 PM
Tags: ,
Categories: JavaScript
Actions: E-mail | Kick it! | DZone it! | del.icio.us
Post Information: Permalink | Comments (11) | Post RSSRSS comment feed

Comments

AIR JORDAN 10 People's Republic of China

Sunday, October 17, 2010 9:44 PM

AIR JORDAN 10

This article gives the light in which we can observe the reality. This is very nice one and gives in-depth information

cheap uggs People's Republic of China

Wednesday, October 20, 2010 11:16 AM

cheap uggs

This article gives the light in which we can observe the reality. This is very nice one and gives in-depth information

christian louboutin shoes People's Republic of China

Thursday, October 21, 2010 6:20 PM

christian louboutin shoes

This article gives the light in which we can observe the reality. This is very nice one and gives in-depth information

materace United States

Saturday, October 23, 2010 9:02 AM

materace

I am quite interesting in this topic hope you will elaborate more on it in future posts

retirement annuities United Kingdom

Tuesday, October 26, 2010 2:55 PM

retirement annuities

Please email me with some tips about how you made your site look this good , I would appreciate it.

how to eat a pomegranate United States

Wednesday, October 27, 2010 12:46 AM

how to eat a pomegranate

This site has got some really useful info on it. Thank you for sharing it with me.

Gadget Insurance United Kingdom

Monday, November 22, 2010 7:51 PM

Gadget Insurance

Please email me with some pointers on how you made your blog look this good , I'd be appreciative!

Cheap iPod Nano Insurance United States

Monday, November 22, 2010 8:20 PM

Cheap iPod Nano Insurance

Could you email me with a few hints & tips on how you made this blog look this awesome, I'd be thankful.

Internet WiFi United States

Wednesday, December 01, 2010 11:43 AM

Internet WiFi

I really like following your blog as the articles are so simple to read and follow. Excellent. Please keep up the good work. Thanks.

Cabling Contractors VA United States

Wednesday, December 01, 2010 11:44 AM

Cabling Contractors VA

Hello, I truly enjoyed reading your post. I found your site from Bing. Will bookmark to return later. Thanks!

Targeted Facebook Fans United States

Thursday, December 09, 2010 10:38 PM

Targeted Facebook Fans

Superb commentary. The opposite day I was told about this web site and sought after to assist you to understand that I have been gratified, going via your posts. I might be signing as much as your weblog's feed and will wait for your next post.

Add comment


(Will show your Gravatar icon)

  Country flag

biuquote
  • Comment
  • Preview
Loading