{"id":40,"date":"2019-03-28T15:53:41","date_gmt":"2019-03-28T15:53:41","guid":{"rendered":"https:\/\/www.kindsonthegenius.com\/javascript\/?p=40"},"modified":"2020-08-06T06:02:23","modified_gmt":"2020-08-06T06:02:23","slug":"07-javascript-switch-statement","status":"publish","type":"post","link":"https:\/\/kindsonthegenius.com\/javascript\/07-javascript-switch-statement\/","title":{"rendered":"JavaScript &#8211; Switch Statement"},"content":{"rendered":"<p>Previously we discussed if-statement under <a href=\"https:\/\/kindsonthegenius.com\/javascript\/06-javascript-conditional-statements\/\">Conditional Statements<\/a>. Now, you could have several if-else-if statements. But this is not always the best solution. Hence, there is an alternative to this. That is the Switch Statement.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>How the Switch Statement Works<\/strong><\/p>\n<p>You can use the switch statement to\u00a0 replace repeated if-else-if statements.<\/p>\n<p>The flowchart of the switch statement is given below.<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-41 \" src=\"https:\/\/kindsonthegenius.com\/javascript\/wp-content\/uploads\/2019\/03\/Switch-Statement-Flowchart.jpg\" alt=\"Switch Statement Flowchart\" width=\"291\" height=\"367\" srcset=\"https:\/\/kindsonthegenius.com\/javascript\/wp-content\/uploads\/2019\/03\/Switch-Statement-Flowchart.jpg 611w, https:\/\/kindsonthegenius.com\/javascript\/wp-content\/uploads\/2019\/03\/Switch-Statement-Flowchart-238x300.jpg 238w\" sizes=\"auto, (max-width: 291px) 100vw, 291px\" \/><\/p>\n<p>&nbsp;<\/p>\n<h4><strong>Syntax<\/strong><\/h4>\n<p>I consists of a switch statement and a number of case statements.<\/p>\n<p>Each case statement is followed by a corresponding block of code (statement). The interpreter checks each case statement against the value of the switch expression. If a match is found, then the particular case statement is executed.<\/p>\n<p>If no match is found for all the case statements, then the default statement is executed.<\/p>\n<p>&nbsp;<\/p>\n<p>The syntax of the Switch statement is given below.<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\">switch (expression) {\r\n   case condition 1: statement 1\r\n   break;\r\n   \r\n   case condition 2: statement 2\r\n   break;\r\n   ...\r\n   \r\n   case condition n: statement n\r\n   break;\r\n   \r\n   default: default statement\r\n}\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>Also notice the <strong>break<\/strong> statement. It&#8217;s just a single line. Just one word: break. This is used to exit the switch statement. So once a match is found and the statement is executed, then the break statement exits the switch statement.<\/p>\n<p>&nbsp;<\/p>\n<h4><strong>Switch Statement Example<\/strong><\/h4>\n<p>An example of switch statement is given in the html file below.<\/p>\n<p>&nbsp;<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #007700;\">&lt;html&gt;<\/span>\r\n<span style=\"color: #007700;\">&lt;head&gt;&lt;title&gt;<\/span>Switch Statement Example<span style=\"color: #007700;\">&lt;\/title&gt;&lt;\/head&gt;<\/span>\r\n   <span style=\"color: #007700;\">&lt;body&gt;<\/span>\r\n   \r\n      <span style=\"color: #007700;\">&lt;script <\/span><span style=\"color: #0000cc;\">type =<\/span> <span style=\"background-color: #fff0f0;\">\"text\/javascript\"<\/span><span style=\"color: #007700;\">&gt;<\/span>\r\n         <span style=\"color: #888888;\">&lt;!--<\/span>\r\n            <span style=\"color: #008800; font-weight: bold;\">var<\/span> grade <span style=\"color: #333333;\">=<\/span> <span style=\"background-color: #fff0f0;\">'B'<\/span>;\r\n            <span style=\"color: #007020;\">document<\/span>.write(<span style=\"background-color: #fff0f0;\">\"Entering switch statement block&lt;br \/&gt;\"<\/span>);\r\n            <span style=\"color: #008800; font-weight: bold;\">switch<\/span> (grade) {\r\n               <span style=\"color: #008800; font-weight: bold;\">case<\/span> <span style=\"background-color: #fff0f0;\">'A'<\/span><span style=\"color: #333333;\">:<\/span> <span style=\"color: #007020;\">document<\/span>.write(<span style=\"background-color: #fff0f0;\">\"Excellent&lt;br \/&gt;\"<\/span>);\r\n               <span style=\"color: #008800; font-weight: bold;\">break<\/span>;\r\n            \r\n               <span style=\"color: #008800; font-weight: bold;\">case<\/span> <span style=\"background-color: #fff0f0;\">'B'<\/span><span style=\"color: #333333;\">:<\/span> <span style=\"color: #007020;\">document<\/span>.write(<span style=\"background-color: #fff0f0;\">\"Very Good&lt;br \/&gt;\"<\/span>);\r\n               <span style=\"color: #008800; font-weight: bold;\">break<\/span>;\r\n            \r\n               <span style=\"color: #008800; font-weight: bold;\">case<\/span> <span style=\"background-color: #fff0f0;\">'C'<\/span><span style=\"color: #333333;\">:<\/span> <span style=\"color: #007020;\">document<\/span>.write(<span style=\"background-color: #fff0f0;\">\"Good&lt;br \/&gt;\"<\/span>);\r\n               <span style=\"color: #008800; font-weight: bold;\">break<\/span>;\r\n            \r\n               <span style=\"color: #008800; font-weight: bold;\">case<\/span> <span style=\"background-color: #fff0f0;\">'D'<\/span><span style=\"color: #333333;\">:<\/span> <span style=\"color: #007020;\">document<\/span>.write(<span style=\"background-color: #fff0f0;\">\"Fair&lt;br \/&gt;\"<\/span>);\r\n               <span style=\"color: #008800; font-weight: bold;\">break<\/span>;\r\n            \r\n               <span style=\"color: #008800; font-weight: bold;\">case<\/span> <span style=\"background-color: #fff0f0;\">'F'<\/span><span style=\"color: #333333;\">:<\/span> <span style=\"color: #007020;\">document<\/span>.write(<span style=\"background-color: #fff0f0;\">\"Fail&lt;br \/&gt;\"<\/span>);\r\n               <span style=\"color: #008800; font-weight: bold;\">break<\/span>;\r\n            \r\n               <span style=\"color: #008800; font-weight: bold;\">default<\/span><span style=\"color: #333333;\">:<\/span>  <span style=\"color: #007020;\">document<\/span>.write(<span style=\"background-color: #fff0f0;\">\"Invalid grade&lt;br \/&gt;\"<\/span>)\r\n            }\r\n         <span style=\"color: #888888;\">\/\/--&gt;<\/span>\r\n      <span style=\"color: #007700;\">&lt;\/script&gt;<\/span>                  \r\n      \r\n      You can try different values for grade\r\n   <span style=\"color: #007700;\">&lt;\/body&gt;<\/span>\r\n<span style=\"color: #007700;\">&lt;\/html&gt;<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>If you view the code above, you will have the output:<\/p>\n<p>&nbsp;<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">Entering switch statement block\r\nVery Good\r\nYou can try different values for grade \r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>Now, assuming you did not use the break statements? Let&#8217;s try it to see what the output will be.<\/p>\n<p>The code without break statements is given below:<\/p>\n<p>&nbsp;<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #007700;\">&lt;html&gt;<\/span>\r\n<span style=\"color: #007700;\">&lt;head&gt;&lt;title&gt;<\/span>Switch Statement Example<span style=\"color: #007700;\">&lt;\/title&gt;&lt;\/head&gt;<\/span>\r\n   <span style=\"color: #007700;\">&lt;body&gt;<\/span>\r\n   \r\n      <span style=\"color: #007700;\">&lt;script <\/span><span style=\"color: #0000cc;\">type =<\/span> <span style=\"background-color: #fff0f0;\">\"text\/javascript\"<\/span><span style=\"color: #007700;\">&gt;<\/span>\r\n         <span style=\"color: #888888;\">&lt;!--<\/span>\r\n            <span style=\"color: #008800; font-weight: bold;\">var<\/span> grade <span style=\"color: #333333;\">=<\/span> <span style=\"background-color: #fff0f0;\">'B'<\/span>;\r\n            <span style=\"color: #007020;\">document<\/span>.write(<span style=\"background-color: #fff0f0;\">\"Entering switch statement block&lt;br \/&gt;\"<\/span>);\r\n            <span style=\"color: #008800; font-weight: bold;\">switch<\/span> (grade) {\r\n               <span style=\"color: #008800; font-weight: bold;\">case<\/span> <span style=\"background-color: #fff0f0;\">'A'<\/span><span style=\"color: #333333;\">:<\/span> <span style=\"color: #007020;\">document<\/span>.write(<span style=\"background-color: #fff0f0;\">\"Excellent&lt;br \/&gt;\"<\/span>);\r\n            \r\n               <span style=\"color: #008800; font-weight: bold;\">case<\/span> <span style=\"background-color: #fff0f0;\">'B'<\/span><span style=\"color: #333333;\">:<\/span> <span style=\"color: #007020;\">document<\/span>.write(<span style=\"background-color: #fff0f0;\">\"Very Good&lt;br \/&gt;\"<\/span>);\r\n            \r\n               <span style=\"color: #008800; font-weight: bold;\">case<\/span> <span style=\"background-color: #fff0f0;\">'C'<\/span><span style=\"color: #333333;\">:<\/span> <span style=\"color: #007020;\">document<\/span>.write(<span style=\"background-color: #fff0f0;\">\"Good&lt;br \/&gt;\"<\/span>);\r\n            \r\n               <span style=\"color: #008800; font-weight: bold;\">case<\/span> <span style=\"background-color: #fff0f0;\">'D'<\/span><span style=\"color: #333333;\">:<\/span> <span style=\"color: #007020;\">document<\/span>.write(<span style=\"background-color: #fff0f0;\">\"Fair&lt;br \/&gt;\"<\/span>);\r\n            \r\n               <span style=\"color: #008800; font-weight: bold;\">case<\/span> <span style=\"background-color: #fff0f0;\">'F'<\/span><span style=\"color: #333333;\">:<\/span> <span style=\"color: #007020;\">document<\/span>.write(<span style=\"background-color: #fff0f0;\">\"Fail&lt;br \/&gt;\"<\/span>);\r\n            \r\n               <span style=\"color: #008800; font-weight: bold;\">default<\/span><span style=\"color: #333333;\">:<\/span>  <span style=\"color: #007020;\">document<\/span>.write(<span style=\"background-color: #fff0f0;\">\"Invalid grade&lt;br \/&gt;\"<\/span>)\r\n            }\r\n         <span style=\"color: #888888;\">\/\/--&gt;<\/span>\r\n      <span style=\"color: #007700;\">&lt;\/script&gt;<\/span>                  \r\n      \r\n      You can try different values for grade\r\n   <span style=\"color: #007700;\">&lt;\/body&gt;<\/span>\r\n<span style=\"color: #007700;\">&lt;\/html&gt;<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>Now, if you view this page, you will see that it displays the result below. Clearly, this does not make any sense. Because we only have one grade.<\/p>\n<p>&nbsp;<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">Entering switch statement block\r\nVery Good\r\nGood\r\nFair\r\nFail\r\nInvalid grade\r\nYou can try different values for grade \r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>So don&#8217;t forget break statement.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Previously we discussed if-statement under Conditional Statements. Now, you could have several if-else-if statements. But this is not always the best solution. Hence, there is &hellip; <\/p>\n","protected":false},"author":395,"featured_media":42,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"footnotes":""},"categories":[2],"tags":[],"class_list":["post-40","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-javascript-tutorials"],"_links":{"self":[{"href":"https:\/\/kindsonthegenius.com\/javascript\/wp-json\/wp\/v2\/posts\/40","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kindsonthegenius.com\/javascript\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kindsonthegenius.com\/javascript\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/javascript\/wp-json\/wp\/v2\/users\/395"}],"replies":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/javascript\/wp-json\/wp\/v2\/comments?post=40"}],"version-history":[{"count":2,"href":"https:\/\/kindsonthegenius.com\/javascript\/wp-json\/wp\/v2\/posts\/40\/revisions"}],"predecessor-version":[{"id":111,"href":"https:\/\/kindsonthegenius.com\/javascript\/wp-json\/wp\/v2\/posts\/40\/revisions\/111"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/javascript\/wp-json\/wp\/v2\/media\/42"}],"wp:attachment":[{"href":"https:\/\/kindsonthegenius.com\/javascript\/wp-json\/wp\/v2\/media?parent=40"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/javascript\/wp-json\/wp\/v2\/categories?post=40"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/javascript\/wp-json\/wp\/v2\/tags?post=40"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}