Is it a good practice to end switch with defaults in JavaScript?

HTMLJavascriptProgramming Scripts

Yes, it is a good practice to end switch with defaults. Always end it with defaults.

Example

Let’s see an example of a switch in JavaScript using defaults:

<html>
   <body>
      <script>
         <!--
            var grade='A';
            document.write("Entering switch block<br />");
            switch (grade) {
               case 'A': document.write("Good job<br />");
               break;

               case 'B': document.write("Pretty good<br />");
               break;

               case 'C': document.write("Passed<br />");
               break;

               case 'D': document.write("Not so good<br />");
               break;

               case 'F': document.write("Failed<br />");
               break;

               default: document.write("Unknown grade<br />")
            }
            document.write("Exiting switch block");
         //-->
      </script>
   </body>
</html>
raja
Published on 26-Feb-2018 09:41:30
Advertisements