{"id":23223,"date":"2024-03-27T10:36:51","date_gmt":"2024-03-27T09:36:51","guid":{"rendered":"https:\/\/nelkodev.com\/blog\/estructuras-de-control-condicionales-if-else-en-javascript-fundamentos\/"},"modified":"2024-06-03T17:32:09","modified_gmt":"2024-06-03T16:32:09","slug":"estructuras-de-control-condicionales-if-else-en-javascript-fundamentos","status":"publish","type":"post","link":"https:\/\/nelkodev.com\/en\/blog\/if-else-conditional-control-structures-in-javascript-basics\/","title":{"rendered":"Conditional if else control structures in JavaScript: Basics"},"content":{"rendered":"<p>If else conditional control structures are essential in any programming language, and JavaScript is no exception. These structures allow us to make decisions based on specific conditions and execute different blocks of code accordingly. In this article, we will explore in detail how to use if else conditional control structures in JavaScript and how to get the most out of this powerful tool.<\/p>\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_80 counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #ffffff;color:#ffffff\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewbox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #ffffff;color:#ffffff\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewbox=\"0 0 24 24\" version=\"1.2\" baseprofile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/nelkodev.com\/en\/blog\/if-else-conditional-control-structures-in-javascript-basics\/#%C2%BFQue_son_las_estructuras_de_control_condicionales_if_else\" >What are if else conditional control structures?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/nelkodev.com\/en\/blog\/if-else-conditional-control-structures-in-javascript-basics\/#Uso_de_operadores_de_comparacion_en_las_estructuras_de_control_condicionales_if_else\" >Using comparison operators in if else conditional control structures<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/nelkodev.com\/en\/blog\/if-else-conditional-control-structures-in-javascript-basics\/#Conclusiones\" >Conclusions<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/nelkodev.com\/en\/blog\/if-else-conditional-control-structures-in-javascript-basics\/#Preguntas_frecuentes\" >Frequently asked questions<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/nelkodev.com\/en\/blog\/if-else-conditional-control-structures-in-javascript-basics\/#%C2%BFCual_es_la_diferencia_entre_una_estructura_if_y_una_estructura_if_else_en_JavaScript\" >What is the difference between an if structure and an if else structure in JavaScript?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/nelkodev.com\/en\/blog\/if-else-conditional-control-structures-in-javascript-basics\/#%C2%BFPuedo_anidar_estructuras_de_control_condicionales_if_else_en_JavaScript\" >Can I nest if else conditional control structures in JavaScript?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/nelkodev.com\/en\/blog\/if-else-conditional-control-structures-in-javascript-basics\/#%C2%BFExisten_otras_estructuras_de_control_condicionales_en_JavaScript_ademas_de_if_else\" >Are there other conditional control structures in JavaScript besides if else?<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n<h2><span class=\"ez-toc-section\" id=\"%C2%BFQue_son_las_estructuras_de_control_condicionales_if_else\"><\/span>What are if else conditional control structures?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>In programming, if else conditional control structures allow us to execute different blocks of code based on a specific condition. If the condition evaluates to true, the code block within the &quot;if&quot; structure is executed. Otherwise, if the condition is false, the code block inside the &quot;else&quot; structure is executed.<\/p>\n<p>For example, if we want to check if a number is greater or less than zero, we can use an if else conditional control structure in JavaScript as follows:<\/p>\n<pre>\n<code>\nlet number = 5; if (number &gt; 0) { console.log(&quot;The number is greater than zero&quot;); } else { console.log(&quot;The number is less than or equal to zero&quot;); }\n<\/code>\n<\/pre>\n<p>In this example, if the variable &quot;number&quot; is greater than zero, the message &quot;The number is greater than zero&quot; will be printed to the console. Otherwise, if the variable &quot;number&quot; is less than or equal to zero, the message &quot;The number is less than or equal to zero&quot; will be printed.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Uso_de_operadores_de_comparacion_en_las_estructuras_de_control_condicionales_if_else\"><\/span>Using comparison operators in if else conditional control structures<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>If else conditional control structures in JavaScript allow us to use comparison operators to evaluate conditions. Some of the most common comparison operators are:<\/p>\n<ul>\n<li><code>==<\/code>: compares whether two values are equal<\/li>\n<li><code>!=<\/code>: compares whether two values are different<\/li>\n<li><code>><\/code>: Compares if the value on the left is greater than the value on the right<\/li>\n<li><code>&lt;<\/code>: Compares if the value on the left is less than the value on the right<\/li>\n<li><code>>=<\/code>- Compares whether the value on the left is greater than or equal to the value on the right<\/li>\n<li><code>&lt;=<\/code>- Compares whether the value on the left is less than or equal to the value on the right<\/li>\n<\/ul>\n<p>We can combine these comparison operators with the logical operators &quot;&amp;&amp;&quot; (and) and &quot;||&quot; (or) to create more complex conditions. For example:<\/p>\n<pre>\n<code>\nlet age = 20; let country = &quot;Spain&quot;; if (age &gt;= 18 &amp;&amp; country == &quot;Spain&quot;) { console.log(&quot;You can vote in the elections in your country&quot;); } else { console.log(&quot;You cannot vote in your country&#039;s elections&quot;); }\n<\/code>\n<\/pre>\n<p>In this case, if the &quot;age&quot; variable is greater than or equal to 18 and the &quot;country&quot; variable is equal to &quot;Spain&quot;, the message &quot;You can vote in your country&#039;s elections&quot; will be printed in the console. Otherwise, the message &quot;You cannot vote in your country&#039;s elections&quot; will be printed.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Conclusiones\"><\/span>Conclusions<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>If else conditional control structures are essential for making decisions in programming. In JavaScript, we can use comparison operators and logical operators to evaluate different conditions and execute the corresponding code. These structures allow us to create more flexible and adaptive programs, and take full advantage of the potential of JavaScript as a programming language.<\/p>\n<p>I hope this article was helpful to you in understanding the basics of if else conditional control structures in JavaScript. If you have any questions or comments, feel free to leave me a message through my <a href=\"https:\/\/nelkodev.com\/en\/contact\/\">website<\/a>. Remember to also visit my <a href=\"https:\/\/nelkodev.com\/en\/portfolio\/\">briefcase<\/a> to discover more content related to web development and digital marketing.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Preguntas_frecuentes\"><\/span>Frequently asked questions<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<h3><span class=\"ez-toc-section\" id=\"%C2%BFCual_es_la_diferencia_entre_una_estructura_if_y_una_estructura_if_else_en_JavaScript\"><\/span>What is the difference between an if structure and an if else structure in JavaScript?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>The difference between an if structure and an if else structure lies in the execution of the code blocks. In an if structure, the code block is only executed if the condition is true. In an if else structure, the code block inside the &quot;if&quot; is executed if the condition is true and the code block inside the &quot;else&quot; is executed if the condition is false.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"%C2%BFPuedo_anidar_estructuras_de_control_condicionales_if_else_en_JavaScript\"><\/span>Can I nest if else conditional control structures in JavaScript?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Yes, in JavaScript it is possible to nest if else conditional control structures. This means that you can place an if else structure inside another if else structure. However, it is important to keep the code organized and readable, avoiding nesting too many structures so as not to complicate the program&#039;s logic.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"%C2%BFExisten_otras_estructuras_de_control_condicionales_en_JavaScript_ademas_de_if_else\"><\/span>Are there other conditional control structures in JavaScript besides if else?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Yes, apart from the if else structure, JavaScript also offers other conditional control structures such as &quot;switch&quot;. The switch structure allows us to evaluate an expression and execute different cases of code based on the value of the expression. This can be especially useful when you have multiple options and want to avoid using multiple if else structures.<\/p>","protected":false},"excerpt":{"rendered":"<p>If else conditional control structures are fundamental in any programming language, and JavaScript is no exception. These structures allow us to make decisions based on specific conditions and execute different blocks of code accordingly. In this article, we will explore in detail how to use if else conditional control structures in JavaScript.<\/p>","protected":false},"author":1,"featured_media":23224,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[420,1123],"tags":[205,792,330,548,784,518,185],"class_list":["post-23223","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-blog","category-fundamentos","tag-blog","tag-condicionales","tag-control","tag-else","tag-estructuras","tag-fundamentos","tag-javascript"],"_links":{"self":[{"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/posts\/23223","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/comments?post=23223"}],"version-history":[{"count":0,"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/posts\/23223\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/media\/23224"}],"wp:attachment":[{"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/media?parent=23223"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/categories?post=23223"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/tags?post=23223"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}