{"id":663,"date":"2024-07-30T19:52:36","date_gmt":"2024-07-30T17:52:36","guid":{"rendered":"https:\/\/blog.sutilweb.eu\/?page_id=663"},"modified":"2024-07-30T19:52:37","modified_gmt":"2024-07-30T17:52:37","slug":"002-condicionales-javascript","status":"publish","type":"page","link":"https:\/\/sutilweb.eu\/index.php\/lenguajes\/javascript\/javascript-practico\/04-estructuras-de-control-en-javascript\/002-condicionales-javascript\/","title":{"rendered":"002. Condicionales Javascript"},"content":{"rendered":"\n<p>En este cap\u00edtulo todos los <strong>operadores relacionales<\/strong> y <strong>l\u00f3gicos<\/strong> que vimos en el cap\u00edtulo anterior van a tomar mayor relevancia.<\/p>\n\n\n\n<!--more-->\n\n\n\n<h2 class=\"wp-block-heading\">Estructuras de control<\/h2>\n\n\n\n<p>Las <strong>estructuras de control<\/strong> son aquellos mecanismos que permite controlar el <strong>flujo<\/strong> de nuestra programaci\u00f3n, de hecho todas las lineas de c\u00f3digo que hemos escrito hasta ahora han sido <strong>estructuras secuenciales<\/strong>, porque se va ejecutando una linea despu\u00e9s de la otra y as\u00ed sucesivamente. Pero llegar\u00e1 un momento en el que vamos a tener que tomar ciertas acciones o no, dependiendo del resultado de evaluar una condici\u00f3n.<\/p>\n\n\n\n<p>Dentro de las <strong>estructuras de control<\/strong> tenemos:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Estructuras secuenciales<\/strong><\/li>\n\n\n\n<li><strong>Estructuras condicionales:<\/strong> dependiendo de la evaluaci\u00f3n de una condici\u00f3n hacemos o no ciertas acciones.<\/li>\n\n\n\n<li><strong>Loops<\/strong> o <strong>estructuras repetitivas<\/strong>.<\/li>\n<\/ul>\n\n\n\n<p>En este cap\u00edtulo vamos a ver las <strong>estructuras condicionales<\/strong>, las m\u00e1s usadas.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">if&#8230; else<\/h2>\n\n\n\n<p>Es una <strong>estructura de programaci\u00f3n<\/strong> que nos permite tomar una decisi\u00f3n, y si la condici\u00f3n se cumple, ejecutar ciertas acciones.<\/p>\n\n\n\n<p><strong>Ejm<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp; &nbsp; &nbsp; &nbsp; let edad = 17;\n&nbsp; &nbsp; &nbsp; &nbsp; if(edad &gt; 17) {\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log(\"Eres mayor de edad\");\n&nbsp; &nbsp; &nbsp; &nbsp; } else {\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log(\"Eres menor de edad\");\n&nbsp; &nbsp; &nbsp; &nbsp; }\n\n&nbsp; &nbsp; &nbsp; &nbsp; let edad2 = 18;\n&nbsp; &nbsp; &nbsp; &nbsp; if(edad2 &gt;= 18) {\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log(\"Eres mayor de edad\");\n&nbsp; &nbsp; &nbsp; &nbsp; } else {\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log(\"Eres menor de edad\");\n&nbsp; &nbsp; &nbsp; &nbsp; }<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">if &#8230; else if &#8230; else<\/h2>\n\n\n\n<p>Podemos anidar varios if, veamos un ejm.<\/p>\n\n\n\n<p><strong>Ejm<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp; &nbsp; &nbsp; &nbsp; \/\/ Madrugada: 0 - 5 hrs\n&nbsp; &nbsp; &nbsp; &nbsp; \/\/ Buenos d\u00edas: 6 - 11 hrs\n&nbsp; &nbsp; &nbsp; &nbsp; \/\/ Buenas tardes: 12 - 18 hrs\n&nbsp; &nbsp; &nbsp; &nbsp; \/\/ Buenas noches: 19 - 23 hrs\n\n&nbsp; &nbsp; &nbsp; &nbsp; let hora = 12;\n\n&nbsp; &nbsp; &nbsp; &nbsp; if (hora &gt;= 0 &amp;&amp; hora &lt;= 5) {\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log(\"Es de madrugada\");\n&nbsp; &nbsp; &nbsp; &nbsp; } else if (hora &gt;= 6 &amp;&amp; hora &lt;= 11) {\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log(\"Buenos d\u00edas\");\n&nbsp; &nbsp; &nbsp; &nbsp; } else if (hora &gt;= 12 &amp;&amp; hora &lt;= 18) {\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log(\"Buenas tardes\");\n&nbsp; &nbsp; &nbsp; &nbsp; } else {\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log(\"Buenas noches\");\n&nbsp; &nbsp; &nbsp; &nbsp; }<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Operador ternario<\/h2>\n\n\n\n<p>Como su nombre dice, el <strong>operador ternario<\/strong> dispone de 3 partes:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Una condici\u00f3n que va entre par\u00e9ntesis.<\/li>\n\n\n\n<li>Un signo de interrogaci\u00f3n en caso de que la condici\u00f3n sea verdadera.<\/li>\n\n\n\n<li>Un signo de dos puntos (:) en el caso de que la condici\u00f3n sea falsa.<\/li>\n<\/ul>\n\n\n\n<p>Su sintaxis ser\u00eda la siguiente:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">(condici\u00f3n) ? verdadero : falso<\/pre>\n\n\n\n<p>Digamos que es la simplificaci\u00f3n de if&#8230; else en una s\u00f3la linea de c\u00f3digo. Si necesitamos varias instrucciones de c\u00f3digo en base a si la condici\u00f3n es verdadera o falsa hay que utilizar if&#8230; else.<\/p>\n\n\n\n<p><strong>Ejm<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp; &nbsp; &nbsp; &nbsp; \/\/ Operador ternario\n        let edad = 18; \n&nbsp; &nbsp; &nbsp; &nbsp; let eresMayor = (edad &gt;= 18)\n  &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ? \"Eres mayor de edad\" \/\/ Parte verdadera\n  &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; : \"Eres menor de edad\"; \/\/ Parte falsa\n\n&nbsp; &nbsp; &nbsp; &nbsp; console.log(eresMayor);<\/pre>\n\n\n\n<p>En la mayor\u00eda de los <strong>lenguajes de programaci\u00f3n<\/strong> (<strong>PHP<\/strong>, <strong>Python<\/strong>&#8230;) existe este <strong>operador<\/strong>. Va a ser importante ya que en muchas librer\u00edas reactivas (<strong>Angular<\/strong>, <strong>Vue<\/strong>&#8230;) es muy frecuente encontrarse este tipo de operadores.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Estructura switch&#8230; case<\/h2>\n\n\n\n<p>La \u00faltima estructura que vamos a ver es la estructura <strong><em>switch&#8230; case<\/em><\/strong>, que nos sirve cuando tengamos una situaci\u00f3n en donde tengamos diferentes valores para una misma variable. Esta estructura se caracteriza por tener casos<\/p>\n\n\n\n<p><strong>Ejm<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp; &nbsp; &nbsp; &nbsp; let dia = 2;\n&nbsp; &nbsp; &nbsp; &nbsp; switch (dia) {\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case 0:\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log(\"Es domingo\")\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case 1:\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log(\"Es lunes\");\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case 2:\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log(\"Es martes\");\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case 3:\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log(\"Es mi\u00e9rcoles\");\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case 4:\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log(\"Es jueves\");\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case 5:\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log(\"Es viernes\");\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case 6:\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log(\"Es s\u00e1bado\");\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; default:\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log(\"El d\u00eda no existe\");\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;\n&nbsp; &nbsp; &nbsp; &nbsp; }\n  &nbsp; &lt;\/script&gt;<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>En este cap\u00edtulo todos los operadores relacionales y l\u00f3gicos que vimos en el cap\u00edtulo anterior van a tomar mayor relevancia.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":658,"menu_order":1,"comment_status":"closed","ping_status":"closed","template":"","meta":{"_uag_custom_page_level_css":"","site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"class_list":["post-663","page","type-page","status-publish","hentry"],"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false},"uagb_author_info":{"display_name":"Sutil Web","author_link":"https:\/\/sutilweb.eu\/index.php\/author\/sutilweb\/"},"uagb_comment_info":0,"uagb_excerpt":"En este cap\u00edtulo todos los operadores relacionales y l\u00f3gicos que vimos en el cap\u00edtulo anterior van a tomar mayor relevancia.","_links":{"self":[{"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/pages\/663","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/comments?post=663"}],"version-history":[{"count":2,"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/pages\/663\/revisions"}],"predecessor-version":[{"id":665,"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/pages\/663\/revisions\/665"}],"up":[{"embeddable":true,"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/pages\/658"}],"wp:attachment":[{"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/media?parent=663"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}