Global GuideLine Home HTML Tutorial JavaScript Tutorial SQL Tutorial CSS Tutorial SEO Tutorial XML Tutorial XSLT Tutorial Articles Tutorial Interview Questions and Answers Guide Jobs GuideLine Bookmark With Google Bookmark With Yahoo Bookmark With FaceBook Bookmark With Live Follow @GlobalGuideLine on Twitter Bookmark Global GuideLine
JavaScript Switch Statement, JavaScript Tutorial, Global Guide Line Technology.
Bookmark this webpage. Bookmark: JavaScript Switch Statement, JavaScript Tutorial Global Guideline is UNITED STATES's favorite website

      Home                    

Basic JavaScript Guide.
 JScript Introduction.            
 JScript Why?                       
 JScript Recognitions.          
 JScript onMouseover.         
 JScript with Buttons.           
 JScript Alerts.                     
 JScript Variables.               
 JScript Operators.               
 JScript Functions.               
 JScript Prompts.                 
 JScript Conditions.             
 JScript Validations.            
 JScript Confirmation Boxes.
 JScript Browser Detection.  
 JScript Redirection.            
 JScript on Links.                 
 JScript Switch. 
 JScript Loops.                     
 JScript Events.                    
 JScript Exception Handling.
 JScript Reserve Characters. 
 JScript with Objects.            
 JScript Math Object.           
 JScript using Dates.             
 JScript using String.            
 JScript using Arrays.            
 JScript Operations.              
 JScript Handling Cookies.   
 JScript in Mapping.             
 JScript Time Out.                
 JScript Summary.                

Learn JavaScript with Examples
Learn JavaScript with hundred of short examples with code and output.
Complete_JScript_Examples.php

JScript Interview Questions & Answers
Learn JavaScript by thousands of Interview Questions & Answers.
JavaScriptQuestions/index.php

JavaScript Tutorial >> JavaScript Switch Statement.


     Back             Next     





Here in JavaScript Tutorial we will learn that a JavaScript Switch Statement enables the execution of one or more JavaScript statements when a specified expression's value matches a label.

Syntax of JavaScript Switch Statement

switch (expression) {
  case Case1 :
    what to do write here...
  case Case2:
    what to do write here...
  ...
  ...
  ...
  default :
    what to do write here...
}

On the above example expression to be evaluated And Case1, Case2 and so on are identifier to be matched against expression. If any case comes equal to expression, execution starts with the "what to do .. " immediately after the colon and continues until it encounters either a break statement in JavaScript switch statement, which is optional, or the end of the JavaScript switch statement comes.

Default clause in JavaScript Switch

At the end of the case we use the default clause to provide a statement to be executed if none of the Cases values matches expression. It can appear anyplace within the JavaScript switch code block.

How much cases can be define in JavaScript Switch?

Zero or more Case blocks may be specified in a JavaScript switch statement. If no Switch Case matches the value of provided expression, and a default case is not supplied in JavaScript switch statement, no statements are executed.

Flow for execution using Switch Statement in JavaScript is as follows

Evaluate the given expression in JavaScript switch statement and look at cases in order until a match is found.

>> If a Case value comes equal to given expression, execute its associated "what to do" block area of JavaScript statements. Then continue execution until a break statement is encountered in JavaScript switch, or the JavaScript switch statement comes on ends. This means that multiple Case blocks are executed if a break statement is not used in the entire JavaScript switch statement.

>> If no Case equals to the provided JavaScript switch expression, go to the switch default case. And if there is no default case, then go to last step of JavaScript Switch Statement.

>> Continue execution at the JavaScript Switch Case statement following the end of the JavaScript switch code block.

Example of JavaScript Switch Statement with break and default.


<script language="javascript">

var date=new Date();

var varExpression = date.getDay();



switch (varExpression) {

 case 0 :

  document.write(" Today is Sunday, have a nice holiday");

  break;

 case 1 :

  document.write(" Today is Monday, have a good working day");

  break;

 case 2 :

  document.write(" Today is Tuesday, have a good working day");

  break; 

 case 3 :

  document.write(" Today is Wednesday, have a good working day");

  break; 

 case 4 :

  document.write(" Today is Thursday, have a good working day");

  break; 

 case 5 :

  document.write(" Today is Friday, have a good working day");

  break; 

 case 6 :

  document.write(" Today is Saturday, have good holiday");

  break; 

 default :

  document.write(" Invalid switch statement in JavaScript Tutorial");

} 

</script>



     Back             Next     
 

JavaScript If Else and Switch Statements Examples



In order to link this page as Reference, click on below text area and pres (CTRL-C) to copy the code in clipboard or pres right click then copy the following lines after that past into your website or Blog.

Get Reference Link To This Page: (copy below code by (CTRL-C) and paste into your website or Blog)
HTML Rendering of above code:

[ About ] [ Contact ] [ Home ]
[ Links ] [ Site Map ] [ Services ] [ Privacy ]

Copyright © 2005-2006 www.globalguideline.com All rights reserved. Join Global Guide Line community. 300 visitors are online now