JavaScript » Statements » switch


The switch statement tests an expression against a number of case options and executes the statements associated with the first one to match. If no match is found, the program looks for a set of default statements to execute, and if these aren't found either, it carries on with the statement immediately following switch.

An optional break statement with each case ensures that once a set of statements has been executed, the program exits switch. If break were omitted, the statements associated with the following case would also be executed:


switch (i)
   case "Chicago" :
      document.write ("Flights to Chicago: Saturdays.");
   case "London" :
      document.write ("Flights to London: Fridays.");
   case "New York" :
      document.write ("Flights to New York: Fridays.");
   case "San Francisco" :
      document.write ("Flights to San Francisco: Wednesdays.");
   default :
      document.write ("Sorry, there are no flights to " + i + ".<BR>");
document.write("Thank you for enquiring with Northern Airlines.<BR>");