The Switch statement in JavaScript

27. October 2009 18:43

Another useful programming tool is the Switch Statement. The beauty of the Switch statement is that only one condition is checked. The result of that condition will lead to a portion of code to be executed. There is no need to go through a chain of “If Else” statements. So if your program will have a lot of possible choices, consider a Switch Statement.

i.e.

<script type= “text/javascript”>

var x= 3

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>

Yes this example probably simpler than what you would use a Switch statement for, but it shows you the point. Variable ‘x’ is equal to three, so the Switch statement will jump to case 3 and print “Three” on the screen. The “break” statement is used after each case to prevent it from running through the code in the following cases.





Comments

11/30/2009 11:58:12 PM #

How to Lose Weight

Hey, I just thought you might wanna know that you're blog's sidebar isn't showing up properly in K-Meleon. I'm using Linux though, so I'm not sure if the problem is from me.

-Melinda

How to Lose Weight United States |

12/23/2009 6:38:17 PM #

hk_webdesign

thanks

hk_webdesign People's Republic of China |

2/19/2010 3:36:21 AM #

stock quotes

I was wondering if you would like to be a guest poster on my blog? In exchange you could put one link the post? Please reply when you get a chance and I will send you my contact details - thanks.

stock quotes United States |

2/19/2010 7:58:59 AM #

Daniel Millions

I didn't see a link anywhere but do you offer advertising? I have a few in the same niche and I would like to add my button somwhere on your page.

Daniel Millions United States |

3/2/2010 4:13:11 AM #

bad credit loans

Most of the time I don’t make comments on websites, but I'd like to say that this post really forced me to do so. Really nice post!

bad credit loans United States |

3/2/2010 11:59:48 AM #

Eugene Briggeman

Very interesting post. Couldn't of written any better. Browsing this post reminds me of my old mate. He constantly kept speaking about this. I will send this post to him. Am sure he will have a good read. Thanks for sharing! Smile

Eugene Briggeman United States |

3/5/2010 12:10:24 PM #

Tess Savcedo

The tester is not solely accountable for quality. That is the occupation of the total team. I believe that part of the reason its easy for teams to get into this groupthink about bugs is because the testers are marginalized into the peripheries and looked at as a deterrent to a greater extent than a assistance to the team -- i.e. a barrier for letting out features that the team genuinely wants to release. If a bug like that lived, the feature should never have been published, period. If you have so many bugs that you are normally making these types of determinations, it suggests that you're attempting to produce too many features simultaneously. Doing this has the show of operating faster, but is in reality slowing you down - as exemplified by the effort of release push backs, patches, bug management.

Tess Savcedo United States |

3/8/2010 8:36:24 PM #

registry cleaner comparison

Nice article. I just stumbled upon your website and wanted to say that I have really enjoyed reading your articles. Anyway I'll be subscribing to your feed and I hope you write again soon.

registry cleaner comparison United States |

3/23/2010 10:13:36 PM #

cheap payday loans

As a Newbie, I am always searching online for articles that can help me. Thank you

cheap payday loans United States |

3/27/2010 4:40:16 AM #

no fax cash advances

I don’t usually reply to posts but I will in this case. WoW Smile

no fax cash advances United States |

3/29/2010 7:37:51 PM #

fast cash advance payday loan

I thought it was going to be some boring old post, but it really compensated for my time. I will post a link to this page on my blog. I am sure my visitors will find that very useful

fast cash advance payday loan United States |

3/30/2010 5:31:13 AM #

personal loans for people with bad credit

Me and my friend were arguing about an issue similar to this! Now I know that I was right. lol! Thanks for the information you post.

personal loans for people with bad credit United States |

4/1/2010 4:07:51 PM #

stretch mark solution

Aw, this was a really quality post. In theory I'd like to write like this too - taking time and real effort to make a good article... but what can I say... I procrastinate alot and never seem to get something done.

stretch mark solution United States |

4/6/2010 12:01:07 AM #

notebook

Great post. I have learned a lot. I am grateful to my friend who told me to visit your blog. Thanks a lot!

notebook United States |

4/28/2010 1:02:04 AM #

Rapidshare

I\'m happy I found this blog, I couldnt discover any info on this subject matter prior to. I also run a site and if you want to ever serious in a little bit of guest writing for me if possible feel free to let me know, i\'m always look for people to check out my site. Please stop by and leave a comment sometime!

Rapidshare United States |

4/29/2010 8:35:17 PM #

fast loan approval

I love what you guys are always up too. Such clever work and reporting! Keep up the great works guys I've added you guys to my blogroll.

fast loan approval United States |

5/13/2010 8:54:06 PM #

free fighting games

hey buddy, fabulous blogpost.. pls continue this great work!!!

free fighting games United States |

5/25/2010 9:49:33 AM #

fast online loans

Hello. Great job. I did not expect this on a Wednesday. This is a good story. Thanks!

fast online loans United States |

6/5/2010 10:41:55 AM #

payday loans

Problems are only opportunities in work clothes.

payday loans United States |

8/11/2010 8:34:03 AM #

Grohe

GROHE products are designed to last and we rigorously test them to guarantee their longevity. This is a core characteristic of the GROHE brand. Our products are engineered by the most experienced designers and research & development professionals in the industry and all our products are tested for a 15-year lifespan. GROHE’s internal testing processes are among the most gruelling in the industry. None of our competitors invest as much in product testing and product life testing.

Grohe United States |

Comments are closed


About the author

 The creators of the Web Design Blog have been creating web designs for over 12 years

Page List