{"id":660,"date":"2024-07-30T19:51:31","date_gmt":"2024-07-30T17:51:31","guid":{"rendered":"https:\/\/blog.sutilweb.eu\/?page_id=660"},"modified":"2024-07-30T19:51:31","modified_gmt":"2024-07-30T17:51:31","slug":"001-tipos-de-operadores-en-javascript","status":"publish","type":"page","link":"https:\/\/sutilweb.eu\/index.php\/lenguajes\/javascript\/javascript-practico\/04-estructuras-de-control-en-javascript\/001-tipos-de-operadores-en-javascript\/","title":{"rendered":"001. Tipos de operadores en Javascript"},"content":{"rendered":"\n<p>En este cap\u00edtulo veremos un concepto muy importante, el concepto de los <strong>operadores<\/strong>. Vamos a tener distintos tipos de <strong>operadores<\/strong>.<\/p>\n\n\n\n<!--more-->\n\n\n\n<h2 class=\"wp-block-heading\">1. Operadores aritm\u00e9ticos<\/h2>\n\n\n\n<p>Encontraremos los siguientes operadores:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Suma<\/strong>: se utiliza el signo m\u00e1s (+).<\/li>\n\n\n\n<li><strong>Resta<\/strong>: se utiliza el signo menos (-).<\/li>\n\n\n\n<li><strong>Producto<\/strong>: se utiliza el s\u00edmbolo de asterisco (*).<\/li>\n\n\n\n<li><strong>Cociente<\/strong>: se utiliza la barra inclinada (\/).<\/li>\n\n\n\n<li><strong>M\u00f3dulo<\/strong>: devuelve el resto o residuo de una divisi\u00f3n. Se utiliza el s\u00edmbolo de porcentaje (%).<\/li>\n\n\n\n<li><strong>Agrupaci\u00f3n<\/strong>: se utilizan los s\u00edmbolos de par\u00e9ntesis ().<\/li>\n<\/ul>\n\n\n\n<p><strong>Ejm<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp; &nbsp; &nbsp; &nbsp; let a = 5 + (5 - 10) * 3;\n&nbsp; &nbsp; &nbsp; &nbsp; console.log(a);<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">2. Operadores relacionales<\/h2>\n\n\n\n<p>Son los que permiten comparar ciertos valores, son los siguientes:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Mayor que: ><\/li>\n\n\n\n<li>Menor que: &lt;<\/li>\n\n\n\n<li>Mayor o igual que: >=<\/li>\n\n\n\n<li>Menor o igual que: &lt;=<\/li>\n\n\n\n<li>Igual en valor: ==<\/li>\n\n\n\n<li>Id\u00e9ntico en valor y tipo: === (la buena pr\u00e1ctica es utilizar este tipo de comparaci\u00f3n)<\/li>\n\n\n\n<li>Distinto en valor: !=<\/li>\n\n\n\n<li>Distinto en valor y tipo: !==<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">3. Operadores de incremento\/decremento<\/h2>\n\n\n\n<p>Veamos un ejm para entender como funcionan.<\/p>\n\n\n\n<p><strong>Ejm<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp; &nbsp; &nbsp; &nbsp; let i = 1;\n\n&nbsp; &nbsp; &nbsp; &nbsp; \/\/ Una forma de expresar el incremento\n&nbsp; &nbsp; &nbsp; &nbsp; i = i+2;\n&nbsp; &nbsp; &nbsp; &nbsp; console.log(i);\n\n&nbsp; &nbsp; &nbsp; &nbsp; \/\/ Otra forma de expresar el incremento\n&nbsp; &nbsp; &nbsp; &nbsp; i += 3\n  &nbsp; &nbsp; &nbsp; console.log(i);  &nbsp;  \n\n&nbsp; &nbsp; &nbsp; &nbsp; i \/= 3\n&nbsp; &nbsp; &nbsp; &nbsp; console.log(i);\n&nbsp; &nbsp; &nbsp; &nbsp;\n&nbsp; &nbsp; &nbsp; &nbsp; i *= 3\n&nbsp; &nbsp; &nbsp; &nbsp; console.log(i);<\/pre>\n\n\n\n<p>Hay 2 casos especiales, que es cuando vamos aumentando o disminuyendo un s\u00f3lo valor por cada vez. Son los denominados operadores unarios. 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; \/\/ Otra forma de expresar el incremento\n&nbsp; &nbsp; &nbsp; &nbsp; i += 3\n&nbsp; &nbsp; &nbsp; &nbsp; console.log(i);\n&nbsp; &nbsp; &nbsp; &nbsp;\n&nbsp; &nbsp; &nbsp; &nbsp; i \/= 3\n&nbsp; &nbsp; &nbsp; &nbsp; console.log(i);\n&nbsp; &nbsp; &nbsp; &nbsp;\n&nbsp; &nbsp; &nbsp; &nbsp; i *= 3\n&nbsp; &nbsp; &nbsp; &nbsp; console.log(i);\n&nbsp; &nbsp; &nbsp; &nbsp;\n&nbsp; &nbsp; &nbsp; &nbsp; i = 2;\n&nbsp; &nbsp; &nbsp; &nbsp; i++;\n&nbsp; &nbsp; &nbsp; &nbsp; console.log(i);\n&nbsp; &nbsp; &nbsp; &nbsp;\n&nbsp; &nbsp; &nbsp; &nbsp; i = 2;\n&nbsp; &nbsp; &nbsp; &nbsp; i--;\n&nbsp; &nbsp; &nbsp; &nbsp; console.log(i);\n&nbsp; &nbsp; &nbsp; &nbsp;\n&nbsp; &nbsp; &nbsp; &nbsp; i = 2;\n&nbsp; &nbsp; &nbsp; &nbsp; ++i;\n&nbsp; &nbsp; &nbsp; &nbsp; console.log(i);\n&nbsp; &nbsp; &nbsp; &nbsp;\n&nbsp; &nbsp; &nbsp; &nbsp; i = 2;\n&nbsp; &nbsp; &nbsp; &nbsp; --i;\n&nbsp; &nbsp; &nbsp; &nbsp; console.log(i);<\/pre>\n\n\n\n<p>Este tipo de <strong>operador<\/strong> lo veremos m\u00e1s habitualmente despu\u00e9s del nombre de la <strong>variable<\/strong>, porque hay una gran diferencia entre ponerlo antes del nombre de la variable y despu\u00e9s. Cuando ponemos el <strong>operador unario<\/strong> antes de la <strong>variable<\/strong> y hacemos un <strong><em>console.log()<\/em><\/strong>, directamente le suma, mientras que si lo ponemos despu\u00e9s, se lo sumar\u00eda la siguiente vez que hici\u00e9ramos un <strong><em>console.log()<\/em><\/strong>. Como recomendaci\u00f3n, utilizar el operador unario despu\u00e9s de la <strong>variable<\/strong>, ya que en tiempo de ejecuci\u00f3n se podr\u00eda generar alg\u00fan error inesperado.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">4. Operadores l\u00f3gicos<\/h2>\n\n\n\n<p>Junto con los <strong>operadores relacionales<\/strong>, tomar\u00e1n m\u00e1s poder en el siguiente cap\u00edtulo de las <strong>estructuras condicionales<\/strong>. Vamos a tener 3 tipos de <strong>operadores l\u00f3gicos<\/strong>.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Negaci\u00f3n (Not)<\/strong>: se usa el s\u00edmbolo de exclamaci\u00f3n (!).<\/li>\n\n\n\n<li><strong>El o: (||)<\/strong>: cuando tengo dos o m\u00e1s condiciones, con que una se cumpla, devuelve true.<\/li>\n\n\n\n<li><strong>El y: (&amp;&amp;)<\/strong>: cuando tengo dos o m\u00e1s condiciones, se tienen que cumplir todas para que devuelva true.<\/li>\n<\/ul>\n\n\n\n<p>Veamos un ejm para entender los conceptos.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">  &nbsp; &nbsp; &nbsp; console.log(!true); \/\/ Devuelve false\n&nbsp; &nbsp; &nbsp; &nbsp; console.log(!false); \/\/ Devuelve true\n&nbsp; &nbsp; &nbsp; &nbsp; console.log((9===9) || (8===9)); \/\/ Devuelve true\n  &nbsp; &nbsp; &nbsp; console.log((9===9) &amp;&amp; (8===9)); \/\/ Devuelve false<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>En este cap\u00edtulo veremos un concepto muy importante, el concepto de los operadores. Vamos a tener distintos tipos de operadores.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":658,"menu_order":0,"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-660","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 veremos un concepto muy importante, el concepto de los operadores. Vamos a tener distintos tipos de operadores.","_links":{"self":[{"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/pages\/660","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=660"}],"version-history":[{"count":2,"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/pages\/660\/revisions"}],"predecessor-version":[{"id":662,"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/pages\/660\/revisions\/662"}],"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=660"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}