WebArtz - The Web Design Forum
Welcome to WebArtz, Guest!

WebArtz is a nice place for discussions related to web designing and coding. We help our members to code their own website templates with HTML and CSS. We give them advice on various issues.

To know more about WebArtz Forum, visit the About Us page.

At the moment, you are viewing the forum as a guest. As a guest you can't make post and participate in discussions. You need to register and become a member of the forum. Click the register link below and become a part of this forum.

Thank You

WebArtz - The Web Design Forum
Welcome to WebArtz, Guest!

WebArtz is a nice place for discussions related to web designing and coding. We help our members to code their own website templates with HTML and CSS. We give them advice on various issues.

To know more about WebArtz Forum, visit the About Us page.

At the moment, you are viewing the forum as a guest. As a guest you can't make post and participate in discussions. You need to register and become a member of the forum. Click the register link below and become a part of this forum.

Thank You


You are not connected. Please login or register

View previous topic View next topic Go down  Message [Page 1 of 1]

1Accespted Javascript - Switch Statement Mon Mar 19, 2012 5:06 am

Mr.Joker

Mr.Joker
Mr. WebArtz
Mr. WebArtz
Javascript - Switch Statement
Working with swtch-case in JavaScript


Hello guys, I am Mr.Joker and today I will teach you how to use switch with javascript. So what for is switch? Let me give you an example. We all know that there is sex as male and female. So, what for is switch there? Every male or female have names, like John, Bill, Mike, and so on. I use switch to manage different possibilities. Here is a example so you understand it better:
Code:
<script type="text/javascript">
var name=window.prompt("What is your name?");
switch(name) {
case "John":
alert("Your name is John, welcome.");
break;
case "Peter":
alert("Your name is Peter, welcome.");
default:
alert("Please enter some name we ahve in our database.");
}
</script>
What we see here? We have a variable that will popup some prompt box where we can enter a value to our variable. So what to do with that value now? We make switch for that variable. Thats why i say switch(variable_name). When I do that, I make a lot of options for value. I say, in case the name is John, alert some message, and then write break. Why break? Well, we need break because if we dont put it there the code will continue executing till the end of code. So we need here to run truough code and when it find a solution it stops right there. I made two cases, one is John, other is Peter. If we type one of them, then one of the codes will be executed, but if we write something else then default thing come. If we write anything else a default is there to run the code and say to user we dont have those names in our database. Simple enough. So guys thats all you have to know about switch.

Notice : This tutorial is copyrighted by WebArtz Forum. You may not publish it on anywhere without written permission from the administrators.

View previous topic View next topic Back to top  Message [Page 1 of 1]

Permissions in this forum:
You cannot reply to topics in this forum