CoffeeScript - switch statement

A switch statement allows a variable to be tested for equality against a list of values. Each value is called a case, and the variable being switched on is checked for each switch case. Here is the syntax of switch in JavaScript.
switch (expression){
   case condition 1: statement(s)
   break;   
   
   case condition 2: statement(s)
   break;
      
   case condition n: statement(s)
   break;
   
   default: statement(s)
}

In JavaScript, after each switch case, we have to use the break statement. If we accidentally forget the break statement, then there is a chance of falling from one switch case to other.
CoffeeScript resolves this problem by using the combination of switch-when-else clauses. Here we have an optional switch expression followed by case statements.
Each case statement have two clauses when and then. The when is followed by condition and then is followed by the set of statements that are to be executed if that particular condition is met. And finally, we have the optional else clause which holds the action for the default condition.
Given below is the syntax of the switch statement in CoffeeScript. We specify the expression without parentheses and we separate the case statements by maintaining proper indentations.
switch expression
   when condition1 then statements
   when condition2 then statements
   when condition3 then statements
   else statements

The following example demonstrates the usage of switch statement in CoffeeScript. Save this code in a file with name switch_example.coffee
name="Ramu"
score=75
message = switch 
   when score>=75 then "Congrats your grade is A"
   when score>=60 then "Your grade is B"
   when score>=50 then "Your grade is C"
   when score>=35 then "Your grade is D"
   else "Your grade is F and you are failed in the exam"
console.log message
Open the command prompt and compile the .coffee file as shown below.
c:\> coffee -c switch_exmple.coffee

On compiling, it gives you the following JavaScript.
// Generated by CoffeeScript 1.10.0
(function() {
  var message, name, score;

  name = "Ramu";

  score = 75;

  message = (function() {
    switch (false) {
      case !(score >= 75):
        return "Congrats your grade is A";
      case !(score >= 60):
        return "Your grade is B";
      case !(score >= 50):
        return "Your grade is C";
      case !(score >= 35):
        return "Your grade is D";
      default:
        return "Your grade is F and you are failed in the exam";
    }
  })();

  console.log(message);

}).call(this);
Now, open the command prompt again and run the CoffeeScript file as −
c:\> coffee switch_exmple.coffee

On executing, the CoffeeScript file produces the following output.
Congrats your grade is A

We can also specify multiple values for a single when clause by separating them using commas (,) in the switch cases.
The following example shows how to write a CoffeeScript switch statement by specifying multiple values for the when clause. Save this code in a file with name switch_multiple_example.coffee
name="Ramu"
score=75
message = switch name
   when "Ramu","Mohammed" then "You have passed the examination with grade A"
   when "John","Julia" then "You have passed the examination with grade is B"
   when "Rajan" then "Sorry you failed in the examination"
   else "No result"
console.log message
Open the command prompt and compile the .coffee file as shown below.
c:\> coffee -c switch_multiple_example.coffee

On compiling, it gives you the following JavaScript.
// Generated by CoffeeScript 1.10.0
(function() {
  var message, name, score;

  name = "Ramu";

  score = 75;

  message = (function() {
    switch (name) {
      case "Ramu":
      case "Mohammed":
        return "You have passed the examination with grade A";
      case "John":
      case "Julia":
        return "You have passed the examination with grade is B";
      case "Rajan":
        return "Sorry you failed in the examination";
      default:
        return "No result";
    }
  })();

  console.log(message);

}).call(this);
Now, open the command prompt again and run the CoffeeScript file as shown below.
c:\> coffee switch_multiple_example.coffee

On executing, the CoffeeScript file produces the following output.
You have passed the examination with grade A 

 Print
 Add Notes
 Bookmark this page