JavaScript switch Statement Example

Updated on     Kisan Patel

In javascript, switch statement is basically to execute the desired choice. The switch statement creates the same output as the if / else statement , but under the hood and in syntax they are pretty different. This is considered faster than if condition.

d = new Date();
ch = d.getMonth();

switch (ch) {
 case 0: document.write("January");
 break;
 case 1: document.write("February");
 break;
 case 2: document.write("March");
 break;
 case 3: document.write("April");
 break;
 case 4: document.write("May");
 break;
 case 5: document.write("June");
 break;
 case 6: document.write("July");
 break;
 case 7: document.write("August");
 break;
 case 8: document.write("September");
 break;
 case 9: document.write("October");
 break;
 case 10: document.write("November");
 break;
 case 11: document.write("December");
 break;
}

In above script, the current month will be printed as output result.

See the Pen ZGZJdZ by Kisan (@pka246) on CodePen.


JavaScript

Leave a Reply