Related post

Inheritance in Java programming with example | Zhullyblog

JavaScript switch case statement - Zhullyblog

Switch case statement



In the previous tutorial, we talked about If else if statement - how it works. But in this tutorial, we would be learning something similar.


The switch case statement is used to perform different action based on different condition. The switch case statement can be used instead of if else if statement. The switch case statement has two important keywords which are case and break.

The case list each condition while the break statement indicates the end of a particular case.

SYNTAX





switch ( expression) {
     case condition 1: statement;
    break;
    case condition 2 : statement;
    break;
    case condition n : statement;
    break;
    default : statement
}





Example




var months = '1';
document.write("Months");
switch (month) {
  case '1' : document.write("January is the first month ");
  break;

  case '2' : document.write("February is the second month") ;
  break;

  case '3' : document.write("March is the third month");
  break;

  case '4' : document.write("April is the fourth month");
  break;

  case '5' : document.write( "May is the fifth month");
  break;
  default: document.write("No data available");
}









Please share












JavaScript Syntax

Where do you place JavaScript in HTML document

JavaScript Variables

JavaScript Strings: How to create and manipulate

JavaScript If, If else, If else if statement

JavaScript Function: How to define and call a function

How to generate output in JS




Comments