{"id":665,"date":"2024-01-04T18:51:00","date_gmt":"2024-01-04T18:51:00","guid":{"rendered":"https:\/\/www.cmsgalaxy.com\/blog\/?p=665"},"modified":"2024-02-05T10:24:39","modified_gmt":"2024-02-05T10:24:39","slug":"javascript-condition","status":"publish","type":"post","link":"https:\/\/www.cmsgalaxy.com\/blog\/javascript-condition\/","title":{"rendered":"JavaScript Condition"},"content":{"rendered":"\n<p>In JavaScript, you can use conditional statements to execute different code blocks based on different conditions. The main types of conditional statements are:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. If Statement:<\/h3>\n\n\n\n<p>The <code>if<\/code> statement executes a block of code if a specified condition is true.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>let x = 10;\n\nif (x &gt; 5) {\n    console.log(\"x is greater than 5\");\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">2. If-Else Statement:<\/h3>\n\n\n\n<p>The <code>if-else<\/code> statement executes one block of code if the condition is true, and another block if the condition is false.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>let x = 3;\n\nif (x % 2 === 0) {\n    console.log(\"x is even\");\n} else {\n    console.log(\"x is odd\");\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">3. Else-If Statement:<\/h3>\n\n\n\n<p>The <code>else if<\/code> statement allows you to specify multiple conditions to be tested.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>let x = 0;\n\nif (x &gt; 0) {\n    console.log(\"x is positive\");\n} else if (x &lt; 0) {\n    console.log(\"x is negative\");\n} else {\n    console.log(\"x is zero\");\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">4. Switch Statement:<\/h3>\n\n\n\n<p>The <code>switch<\/code> statement allows you to test a variable against multiple values and execute different code blocks based on which value it equals to.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>let day = \"Monday\";\n\nswitch (day) {\n    case \"Monday\":\n        console.log(\"It's Monday!\");\n        break;\n    case \"Tuesday\":\n        console.log(\"It's Tuesday!\");\n        break;\n    default:\n        console.log(\"It's another day of the week\");\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">5. Ternary Operator:<\/h3>\n\n\n\n<p>The ternary operator <code>? :<\/code> is a concise way to write conditional statements.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>let x = 10;\nlet message = (x &gt; 5) ? \"x is greater than 5\" : \"x is less than or equal to 5\";\nconsole.log(message);<\/code><\/pre>\n\n\n\n<p>Conditional statements are fundamental for controlling the flow of your JavaScript code, allowing you to execute different blocks of code based on specific conditions. They are powerful tools for building dynamic and interactive applications.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">6. Switch Statements:<\/h3>\n\n\n\n<p>Switch statements in JavaScript provide a way to execute different code blocks based on the value of an expression. The syntax of a switch statement looks like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>switch (expression) {\n  case value1:\n    \/\/ Code block to be executed if expression matches value1\n    break;\n  case value2:\n    \/\/ Code block to be executed if expression matches value2\n    break;\n  \/\/ Additional cases as needed\n  default:\n    \/\/ Code block to be executed if expression doesn't match any case\n}<\/code><\/pre>\n\n\n\n<p>Here&#8217;s how it works:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The <code>switch<\/code> keyword is followed by the expression whose value you want to test.<\/li>\n\n\n\n<li>Each <code>case<\/code> specifies a value to compare the expression against. If the expression matches a <code>case<\/code> value, the corresponding code block is executed.<\/li>\n\n\n\n<li>The <code>break<\/code> statement is used to exit the switch block once a match is found. If omitted, execution will continue to the next case, even if the condition doesn&#8217;t match.<\/li>\n\n\n\n<li>The <code>default<\/code> case is optional and is executed if none of the cases match the expression.<\/li>\n<\/ul>\n\n\n\n<p>Example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>let day = 3;\nlet dayName;\n\nswitch (day) {\n  case 1:\n    dayName = 'Monday';\n    break;\n  case 2:\n    dayName = 'Tuesday';\n    break;\n  case 3:\n    dayName = 'Wednesday';\n    break;\n  case 4:\n    dayName = 'Thursday';\n    break;\n  case 5:\n    dayName = 'Friday';\n    break;\n  case 6:\n    dayName = 'Saturday';\n    break;\n  case 7:\n    dayName = 'Sunday';\n    break;\n  default:\n    dayName = 'Invalid day';\n}\n\nconsole.log(dayName); \/\/ Output: Wednesday<\/code><\/pre>\n\n\n\n<p>Switch statements are useful when you have a single expression with multiple possible values and want to execute different code based on each value. They provide a cleaner and more readable alternative to multiple <code>if-else<\/code> statements in such cases.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In JavaScript, you can use conditional statements to execute different code blocks based on different conditions. The main types of<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-665","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/www.cmsgalaxy.com\/blog\/wp-json\/wp\/v2\/posts\/665","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.cmsgalaxy.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.cmsgalaxy.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.cmsgalaxy.com\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/www.cmsgalaxy.com\/blog\/wp-json\/wp\/v2\/comments?post=665"}],"version-history":[{"count":1,"href":"https:\/\/www.cmsgalaxy.com\/blog\/wp-json\/wp\/v2\/posts\/665\/revisions"}],"predecessor-version":[{"id":666,"href":"https:\/\/www.cmsgalaxy.com\/blog\/wp-json\/wp\/v2\/posts\/665\/revisions\/666"}],"wp:attachment":[{"href":"https:\/\/www.cmsgalaxy.com\/blog\/wp-json\/wp\/v2\/media?parent=665"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.cmsgalaxy.com\/blog\/wp-json\/wp\/v2\/categories?post=665"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.cmsgalaxy.com\/blog\/wp-json\/wp\/v2\/tags?post=665"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}