Javascript TUTORIAL


Switch statement break


The switch statement continues to execute the code until in encounters the break statement. If you do nnot include a break statement, it will continue to execute the code in the next case statement.

Carefully take a look at the following example.

  1. <html>
  2. <body>
  3. <script type="text/javascript">
  4. <!--
  5. /*
  6. ********************************************************
  7. Reference Designer.com
  8. Example switch statement - no break
  9. ********************************************************
  10. */
  11. var height = 62;
  12.  
  13.  
  14. switch (height)
  15. {
  16. case 58:
  17. case 59:
  18. document.write("Height is less than 5 feet");
  19. break;
  20. case 60:
  21. document.write("Height is equal to 5 feet");
  22. break;
  23. case 61:
  24. case 62: document.write("Height is equal 62 inches <br />");
  25. case 63:
  26. document.write("height is more than 5 feet");
  27. break;
  28. default:
  29. document.write("height is out of 58 inch to 63 inch ranhe");
  30. }
  31.  
  32. //-->
  33. </script>
  34. </body>
  35. </html>


This code will produce the following output


Height is equal 62 inches
height is more than 5 feet 



Notice that after executing the statement next to case 62 :, it continues to execute the code next to case 63:.

You can try this example online here.

Javascript case break