JavaScript Switch Statement, JavaScript Tutorial, Global Guide Line Technology.
Forum | Blog |


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.                

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");


 case 1 :

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


 case 2 :

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


 case 3 :

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


 case 4 :

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


 case 5 :

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


 case 6 :

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


 default :

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



     Back             Next     

JavaScript If Else and Switch Statements Examples

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

Copyright 2005 -  2021 All rights reserved. (Best viewed in IE 6.0+ or Firefox 2.0+ at 1024 * 768 or higher) 553 visitors are online now