{"id":22917,"date":"2024-03-26T09:30:06","date_gmt":"2024-03-26T08:30:06","guid":{"rendered":"https:\/\/nelkodev.com\/blog\/estructuras-de-control-operador-ternario-en-javascript\/"},"modified":"2024-06-03T17:29:55","modified_gmt":"2024-06-03T16:29:55","slug":"estructuras-de-control-operador-ternario-en-javascript","status":"publish","type":"post","link":"https:\/\/nelkodev.com\/en\/blog\/ternary-operator-control-structures-in-javascript\/","title":{"rendered":"Control Structures: Ternary Operator in JavaScript"},"content":{"rendered":"<p>In the world of programming, control structures are a fundamental part of ensuring that our programs run efficiently and correctly. One of these structures is the ternary operator, which allows us to make decisions based on a specific condition. In this article, we will explore the basics of the ternary operator in JavaScript and how we can use it in our applications.<\/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\/ternary-operator-control-structures-in-javascript\/#%C2%BFQue_es_el_operador_ternario\" >What is the ternary operator?<\/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\/ternary-operator-control-structures-in-javascript\/#Usando_el_operador_ternario_en_JavaScript\" >Using the ternary operator in JavaScript<\/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\/ternary-operator-control-structures-in-javascript\/#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\/ternary-operator-control-structures-in-javascript\/#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\/ternary-operator-control-structures-in-javascript\/#%C2%BFQue_es_una_estructura_de_control_en_programacion\" >What is a control structure in programming?<\/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\/ternary-operator-control-structures-in-javascript\/#%C2%BFCual_es_la_diferencia_entre_el_operador_ternario_y_la_estructura_if-else_tradicional\" >What is the difference between the ternary operator and the traditional if-else structure?<\/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\/ternary-operator-control-structures-in-javascript\/#%C2%BFPuedo_utilizar_el_operador_ternario_para_realizar_multiples_verificaciones\" >Can I use the ternary operator to perform multiple checks?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/nelkodev.com\/en\/blog\/ternary-operator-control-structures-in-javascript\/#%C2%BFCual_es_la_ventaja_de_utilizar_el_operador_ternario_en_JavaScript\" >What is the advantage of using the ternary operator in JavaScript?<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n<h2><span class=\"ez-toc-section\" id=\"%C2%BFQue_es_el_operador_ternario\"><\/span>What is the ternary operator?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The ternary operator is a concise way to write an if-else control structure in JavaScript. Unlike the traditional if-else structure, which uses keywords such as &quot;if&quot;, &quot;else&quot;, and &quot;else if&quot;, the ternary operator is represented by the symbol &quot;?&quot; and &quot;:&quot; to represent the true and false branches of the condition.<\/p>\n<pre><code>condition ? true_expression : false_expression;<\/code><\/pre>\n<p>In this structure, the &quot;condition&quot; is a logical expression that evaluates to true or false. Depending on the result of this evaluation, &quot;true_expression&quot; is returned if it is true or &quot;false_expression&quot; if it is false. This way of writing the code can be very useful when we want to make a quick decision based on a condition.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Usando_el_operador_ternario_en_JavaScript\"><\/span>Using the ternary operator in JavaScript<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>To better understand how the ternary operator works, let&#039;s look at a practical example. Suppose we want to assign a message to a variable called &quot;greeting&quot; depending on the value of another variable called &quot;time&quot;. If &quot;hour&quot; is less than 12, we want to assign the message &quot;Good morning&quot;, otherwise we want to assign the message &quot;Good afternoon&quot;. Here is what the structure would look like using the ternary operator:<\/p>\n<pre><code>var hour = new Date().getHours(); var greeting = time &lt; 12 ? &quot;Good day good evening&quot;;<\/code><\/pre>\n<p>In this example, we use the &quot;getHours()&quot; method of the &quot;Date&quot; class to get the current time. Then, we use the ternary operator to assign the correct message to the \u201cgreeting\u201d variable depending on the value of \u201ctime.\u201d If &quot;hour&quot; is less than 12, the message &quot;Good morning&quot; is assigned, otherwise the message &quot;Good afternoon&quot; is assigned.<\/p>\n<p>The ternary operator can also be nested to perform multiple checks. For example, we can use it to assign different values to a variable depending on multiple conditions. Here is an example:<\/p>\n<pre><code>var age = 25; var message = age &gt;= 18 ? &quot;Are you of legal age&quot;: age &gt;= 13? &quot;You are a teenager&quot;: &quot;You are a child&quot;;<\/code><\/pre>\n<p>In this case, we use the ternary operator to perform two checks. First, we check if &quot;age&quot; is greater than or equal to 18, in which case the message &quot;You are of age&quot; is assigned. If that condition is not met, a second check is performed to determine if &quot;age&quot; is greater than or equal to 13. If this second condition is met, the message &quot;You are a teenager&quot; is assigned. Otherwise, the message &quot;You are a boy&quot; is assigned.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Conclusiones\"><\/span>Conclusions<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The ternary operator in JavaScript is a powerful tool that allows us to simplify our control structures and make quick decisions based on conditions. Learning to use it correctly can help us write more readable and concise code. I hope this article was helpful and that you now feel more comfortable using the ternary operator in your JavaScript projects.<\/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%BFQue_es_una_estructura_de_control_en_programacion\"><\/span>What is a control structure in programming?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>A control structure is a construct in a programming language that allows us to control the flow of execution of a program. It allows us to make decisions and take actions based on certain conditions.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"%C2%BFCual_es_la_diferencia_entre_el_operador_ternario_y_la_estructura_if-else_tradicional\"><\/span>What is the difference between the ternary operator and the traditional if-else structure?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>The ternary operator is a more concise way to write an if-else control structure. While the traditional if-else structure uses keywords like &quot;if&quot;, &quot;else&quot; and &quot;else if&quot; to represent the different branches of the condition, the ternary operator uses the symbol &quot;?&quot; and &quot;:&quot; to represent these branches more compactly.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"%C2%BFPuedo_utilizar_el_operador_ternario_para_realizar_multiples_verificaciones\"><\/span>Can I use the ternary operator to perform multiple checks?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Yes, the ternary operator can be nested to perform multiple checks. This allows us to assign different values to a variable depending on various conditions.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"%C2%BFCual_es_la_ventaja_de_utilizar_el_operador_ternario_en_JavaScript\"><\/span>What is the advantage of using the ternary operator in JavaScript?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>The main advantage of using the ternary operator in JavaScript is that it allows you to write cleaner and more concise code. Instead of having to write multiple lines of if-else code, we can make quick decisions and assign values more efficiently using the ternary operator.<\/p>","protected":false},"excerpt":{"rendered":"<p>In the world of programming, control structures are a fundamental part of making our programs run efficiently and correctly. One of these structures is the ternary operator, which allows us to make decisions based on a specific condition. In this article, we will explore the basics of the ternary operator in JavaScript [\u2026]<\/p>","protected":false},"author":1,"featured_media":22918,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[420,1123],"tags":[205,330,784,518,185,808,807],"class_list":["post-22917","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-blog","category-fundamentos","tag-blog","tag-control","tag-estructuras","tag-fundamentos","tag-javascript","tag-operador","tag-ternario"],"_links":{"self":[{"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/posts\/22917","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=22917"}],"version-history":[{"count":0,"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/posts\/22917\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/media\/22918"}],"wp:attachment":[{"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/media?parent=22917"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/categories?post=22917"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/tags?post=22917"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}