{"id":7143,"date":"2021-07-28T12:24:40","date_gmt":"2021-07-28T12:24:40","guid":{"rendered":"https:\/\/www.scmgalaxy.com\/tutorials\/?p=7143"},"modified":"2022-04-13T16:30:48","modified_gmt":"2022-04-13T16:30:48","slug":"what-is-the-if-statement-and-switch-statement-in-javascript","status":"publish","type":"post","link":"https:\/\/www.scmgalaxy.com\/tutorials\/what-is-the-if-statement-and-switch-statement-in-javascript\/","title":{"rendered":"What is the IF statement And Switch Statement in JavaScript?"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">If Statement<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">It is used to execute an instruction or block of instructions only if a condition is fulfilled.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Syntax: &#8211;  <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nif (condition)\n      {\n           block of statements;\n      }\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">If statement with logical operator<\/h2>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nif ( (condition1) &amp;amp;&amp;amp; (condition2) )\n     {\n        block of statements;\n     }\n\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">If else Statement<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">if\u2026 else statement is used when a different sequence of instructions is to be executed depending<br>on the logical value(True\/False) of the condition evaluated.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Syntax: &#8211;<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nif(condition)\n   {\n     Statement_1_Block;\n   }\nelse\n   {\n     Statement_2_Block;\n   }\n   Statement_3;\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">Else If Statement<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">To show a multi-way decision based on several conditions, we use else if statement.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Syntax: &#8211;<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nIf(condition_1)\n   {\n     statements_1_Block;\n   }\nelse if(condition_2)\n   {\n     statement_2_Blocks;\n   }\nelse if(condition_n)\n   {\n     Statements_n_Block;\n   }\nelse\n statements_x;\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">Switch Statement<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Check several possible constant values for an expression.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Syntax: &#8211;<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nswitch(expression){\n      case expression 1:\n            block of statements 1;\n            break;\n      case expression2;:\n            block of statements 2;\n            break;\n            .\n            .\n      default:\n            default block of instructions;\n      }\n\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">All the cases will be evaluated if you don&#8217;t use a break statement.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nswitch(expression){\n            case expression 1:\n                 block of statements 1;\n                 break;\n            case expression 2:\n            case expression 3:\n                 block of statements 2;\n                 break;\n            default:\n                 default block of instructions;\n            }\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>If Statement It is used to execute an instruction or block of instructions only if a condition is fulfilled. Syntax: [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":7154,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_joinchat":[],"footnotes":""},"categories":[16],"tags":[5665,5692,5095,5592],"class_list":["post-7143","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-javascript","tag-condition-statement","tag-if-statement","tag-javascript","tag-php-operators"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>What is the IF statement And Switch Statement in JavaScript? - scmGalaxy<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.scmgalaxy.com\/tutorials\/what-is-the-if-statement-and-switch-statement-in-javascript\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"What is the IF statement And Switch Statement in JavaScript? - scmGalaxy\" \/>\n<meta property=\"og:description\" content=\"If Statement It is used to execute an instruction or block of instructions only if a condition is fulfilled. Syntax: [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.scmgalaxy.com\/tutorials\/what-is-the-if-statement-and-switch-statement-in-javascript\/\" \/>\n<meta property=\"og:site_name\" content=\"scmGalaxy\" \/>\n<meta property=\"article:published_time\" content=\"2021-07-28T12:24:40+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-04-13T16:30:48+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.scmgalaxy.com\/tutorials\/wp-content\/uploads\/2021\/07\/if-else-statement-e1627475062975.png\" \/>\n\t<meta property=\"og:image:width\" content=\"300\" \/>\n\t<meta property=\"og:image:height\" content=\"171\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"rajeshkumar\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"rajeshkumar\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/what-is-the-if-statement-and-switch-statement-in-javascript\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/what-is-the-if-statement-and-switch-statement-in-javascript\\\/\"},\"author\":{\"name\":\"rajeshkumar\",\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/#\\\/schema\\\/person\\\/a40e0dd38feee7c2779460a804d49361\"},\"headline\":\"What is the IF statement And Switch Statement in JavaScript?\",\"datePublished\":\"2021-07-28T12:24:40+00:00\",\"dateModified\":\"2022-04-13T16:30:48+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/what-is-the-if-statement-and-switch-statement-in-javascript\\\/\"},\"wordCount\":111,\"commentCount\":0,\"image\":{\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/what-is-the-if-statement-and-switch-statement-in-javascript\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/wp-content\\\/uploads\\\/2021\\\/07\\\/if-else-statement-e1627475062975.png\",\"keywords\":[\"condition statement\",\"If statement\",\"Javascript\",\"PHP Operators\"],\"articleSection\":[\"JavaScript\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/what-is-the-if-statement-and-switch-statement-in-javascript\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/what-is-the-if-statement-and-switch-statement-in-javascript\\\/\",\"url\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/what-is-the-if-statement-and-switch-statement-in-javascript\\\/\",\"name\":\"What is the IF statement And Switch Statement in JavaScript? - scmGalaxy\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/what-is-the-if-statement-and-switch-statement-in-javascript\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/what-is-the-if-statement-and-switch-statement-in-javascript\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/wp-content\\\/uploads\\\/2021\\\/07\\\/if-else-statement-e1627475062975.png\",\"datePublished\":\"2021-07-28T12:24:40+00:00\",\"dateModified\":\"2022-04-13T16:30:48+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/#\\\/schema\\\/person\\\/a40e0dd38feee7c2779460a804d49361\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/what-is-the-if-statement-and-switch-statement-in-javascript\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/what-is-the-if-statement-and-switch-statement-in-javascript\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/what-is-the-if-statement-and-switch-statement-in-javascript\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/wp-content\\\/uploads\\\/2021\\\/07\\\/if-else-statement-e1627475062975.png\",\"contentUrl\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/wp-content\\\/uploads\\\/2021\\\/07\\\/if-else-statement-e1627475062975.png\",\"width\":300,\"height\":171},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/what-is-the-if-statement-and-switch-statement-in-javascript\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"What is the IF statement And Switch Statement in JavaScript?\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/#website\",\"url\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/\",\"name\":\"scmGalaxy\",\"description\":\"A Platform for Software Engineers\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/#\\\/schema\\\/person\\\/a40e0dd38feee7c2779460a804d49361\",\"name\":\"rajeshkumar\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/82337e1fc8642c7f36795aa0775af055a4f38882994004b593efd31758d4434e?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/82337e1fc8642c7f36795aa0775af055a4f38882994004b593efd31758d4434e?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/82337e1fc8642c7f36795aa0775af055a4f38882994004b593efd31758d4434e?s=96&d=mm&r=g\",\"caption\":\"rajeshkumar\"},\"url\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/author\\\/rajeshkumar\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"What is the IF statement And Switch Statement in JavaScript? - scmGalaxy","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.scmgalaxy.com\/tutorials\/what-is-the-if-statement-and-switch-statement-in-javascript\/","og_locale":"en_US","og_type":"article","og_title":"What is the IF statement And Switch Statement in JavaScript? - scmGalaxy","og_description":"If Statement It is used to execute an instruction or block of instructions only if a condition is fulfilled. Syntax: [&hellip;]","og_url":"https:\/\/www.scmgalaxy.com\/tutorials\/what-is-the-if-statement-and-switch-statement-in-javascript\/","og_site_name":"scmGalaxy","article_published_time":"2021-07-28T12:24:40+00:00","article_modified_time":"2022-04-13T16:30:48+00:00","og_image":[{"width":300,"height":171,"url":"https:\/\/www.scmgalaxy.com\/tutorials\/wp-content\/uploads\/2021\/07\/if-else-statement-e1627475062975.png","type":"image\/png"}],"author":"rajeshkumar","twitter_card":"summary_large_image","twitter_misc":{"Written by":"rajeshkumar","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.scmgalaxy.com\/tutorials\/what-is-the-if-statement-and-switch-statement-in-javascript\/#article","isPartOf":{"@id":"https:\/\/www.scmgalaxy.com\/tutorials\/what-is-the-if-statement-and-switch-statement-in-javascript\/"},"author":{"name":"rajeshkumar","@id":"https:\/\/www.scmgalaxy.com\/tutorials\/#\/schema\/person\/a40e0dd38feee7c2779460a804d49361"},"headline":"What is the IF statement And Switch Statement in JavaScript?","datePublished":"2021-07-28T12:24:40+00:00","dateModified":"2022-04-13T16:30:48+00:00","mainEntityOfPage":{"@id":"https:\/\/www.scmgalaxy.com\/tutorials\/what-is-the-if-statement-and-switch-statement-in-javascript\/"},"wordCount":111,"commentCount":0,"image":{"@id":"https:\/\/www.scmgalaxy.com\/tutorials\/what-is-the-if-statement-and-switch-statement-in-javascript\/#primaryimage"},"thumbnailUrl":"https:\/\/www.scmgalaxy.com\/tutorials\/wp-content\/uploads\/2021\/07\/if-else-statement-e1627475062975.png","keywords":["condition statement","If statement","Javascript","PHP Operators"],"articleSection":["JavaScript"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.scmgalaxy.com\/tutorials\/what-is-the-if-statement-and-switch-statement-in-javascript\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.scmgalaxy.com\/tutorials\/what-is-the-if-statement-and-switch-statement-in-javascript\/","url":"https:\/\/www.scmgalaxy.com\/tutorials\/what-is-the-if-statement-and-switch-statement-in-javascript\/","name":"What is the IF statement And Switch Statement in JavaScript? - scmGalaxy","isPartOf":{"@id":"https:\/\/www.scmgalaxy.com\/tutorials\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.scmgalaxy.com\/tutorials\/what-is-the-if-statement-and-switch-statement-in-javascript\/#primaryimage"},"image":{"@id":"https:\/\/www.scmgalaxy.com\/tutorials\/what-is-the-if-statement-and-switch-statement-in-javascript\/#primaryimage"},"thumbnailUrl":"https:\/\/www.scmgalaxy.com\/tutorials\/wp-content\/uploads\/2021\/07\/if-else-statement-e1627475062975.png","datePublished":"2021-07-28T12:24:40+00:00","dateModified":"2022-04-13T16:30:48+00:00","author":{"@id":"https:\/\/www.scmgalaxy.com\/tutorials\/#\/schema\/person\/a40e0dd38feee7c2779460a804d49361"},"breadcrumb":{"@id":"https:\/\/www.scmgalaxy.com\/tutorials\/what-is-the-if-statement-and-switch-statement-in-javascript\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.scmgalaxy.com\/tutorials\/what-is-the-if-statement-and-switch-statement-in-javascript\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.scmgalaxy.com\/tutorials\/what-is-the-if-statement-and-switch-statement-in-javascript\/#primaryimage","url":"https:\/\/www.scmgalaxy.com\/tutorials\/wp-content\/uploads\/2021\/07\/if-else-statement-e1627475062975.png","contentUrl":"https:\/\/www.scmgalaxy.com\/tutorials\/wp-content\/uploads\/2021\/07\/if-else-statement-e1627475062975.png","width":300,"height":171},{"@type":"BreadcrumbList","@id":"https:\/\/www.scmgalaxy.com\/tutorials\/what-is-the-if-statement-and-switch-statement-in-javascript\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.scmgalaxy.com\/tutorials\/"},{"@type":"ListItem","position":2,"name":"What is the IF statement And Switch Statement in JavaScript?"}]},{"@type":"WebSite","@id":"https:\/\/www.scmgalaxy.com\/tutorials\/#website","url":"https:\/\/www.scmgalaxy.com\/tutorials\/","name":"scmGalaxy","description":"A Platform for Software Engineers","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.scmgalaxy.com\/tutorials\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.scmgalaxy.com\/tutorials\/#\/schema\/person\/a40e0dd38feee7c2779460a804d49361","name":"rajeshkumar","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/82337e1fc8642c7f36795aa0775af055a4f38882994004b593efd31758d4434e?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/82337e1fc8642c7f36795aa0775af055a4f38882994004b593efd31758d4434e?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/82337e1fc8642c7f36795aa0775af055a4f38882994004b593efd31758d4434e?s=96&d=mm&r=g","caption":"rajeshkumar"},"url":"https:\/\/www.scmgalaxy.com\/tutorials\/author\/rajeshkumar\/"}]}},"_links":{"self":[{"href":"https:\/\/www.scmgalaxy.com\/tutorials\/wp-json\/wp\/v2\/posts\/7143","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.scmgalaxy.com\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.scmgalaxy.com\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.scmgalaxy.com\/tutorials\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.scmgalaxy.com\/tutorials\/wp-json\/wp\/v2\/comments?post=7143"}],"version-history":[{"count":1,"href":"https:\/\/www.scmgalaxy.com\/tutorials\/wp-json\/wp\/v2\/posts\/7143\/revisions"}],"predecessor-version":[{"id":7156,"href":"https:\/\/www.scmgalaxy.com\/tutorials\/wp-json\/wp\/v2\/posts\/7143\/revisions\/7156"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.scmgalaxy.com\/tutorials\/wp-json\/wp\/v2\/media\/7154"}],"wp:attachment":[{"href":"https:\/\/www.scmgalaxy.com\/tutorials\/wp-json\/wp\/v2\/media?parent=7143"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.scmgalaxy.com\/tutorials\/wp-json\/wp\/v2\/categories?post=7143"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.scmgalaxy.com\/tutorials\/wp-json\/wp\/v2\/tags?post=7143"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}