{"id":6667,"date":"2026-04-13T11:50:46","date_gmt":"2026-04-13T11:50:46","guid":{"rendered":"http:\/\/the-codest.localhost\/dictionary\/ternary-operator\/"},"modified":"2026-04-13T11:50:50","modified_gmt":"2026-04-13T11:50:50","slug":"operador-ternario","status":"publish","type":"dictionary","link":"https:\/\/thecodest.co\/es\/dictionary\/ternary-operator\/","title":{"rendered":"Operador ternario"},"content":{"rendered":"<h1>Operador ternario<\/h1>\n<p>El operador ternario, tambi\u00e9n conocido como operador condicional, es una forma abreviada de escribir una sentencia if-else en una sola l\u00ednea de texto. <a href=\"https:\/\/thecodest.co\/es\/dictionary\/what-is-code-refactoring\/\">c\u00f3digo<\/a>. Toma tres operandos: una condici\u00f3n, un valor a devolver si la condici\u00f3n es verdadera y un valor a devolver si la condici\u00f3n es falsa. La sintaxis del operador ternario es la siguiente:<\/p>\n<p><code>condici\u00f3n ? valor_si_verdadero : valor_si_falso<\/code><\/p>\n<p>Primero se eval\u00faa la condici\u00f3n. Si es verdadera, se devuelve la expresi\u00f3n value_if_true. Si es falsa, se devuelve la expresi\u00f3n value_if_false. El operador ternario se utiliza a menudo como una alternativa m\u00e1s concisa a una sentencia if-else en situaciones en las que la condici\u00f3n es simple y el c\u00f3digo resultante es f\u00e1cil de leer.<\/p>\n<p>Por ejemplo, considere la siguiente sentencia if-else:<\/p>\n<p><code>if (x &gt; 0) { y = x; } else { y = -x; }<\/code><\/p>\n<p>Esto se puede reescribir utilizando el operador ternario como:<\/p>\n<p><code>y = (x &gt; 0) ? x : -x;<\/code><\/p>\n<p>El operador ternario tambi\u00e9n puede anidarse para manejar condiciones m\u00e1s complejas. Por ejemplo:<\/p>\n<p><code>resultado = (x &gt; 0) ? \"Positivo\" : (x &lt; 0) ? &quot;Negativo&quot; : &quot;Cero&quot;;<\/code><\/p>\n<p>Este c\u00f3digo comprueba si x es mayor que 0. Si lo es, se devuelve el valor \"Positivo\". Si no, el c\u00f3digo comprueba si x es menor que 0. Si lo es, se devuelve el valor \"Negativo\". Si x no es ni mayor ni menor que 0, se devuelve el valor \"Cero\".<\/p>\n<p>Aunque el operador ternario puede hacer que el c\u00f3digo sea m\u00e1s conciso y f\u00e1cil de leer en algunos casos, tambi\u00e9n puede hacer que el c\u00f3digo sea m\u00e1s dif\u00edcil de entender si se utiliza en exceso o de forma inadecuada. Es importante utilizar el operador ternario con criterio y dar prioridad a la claridad y mantenibilidad del c\u00f3digo frente a la brevedad.<\/p>","protected":false},"excerpt":{"rendered":"<p>Conozca el operador ternario, una forma abreviada de escribir sentencias if-else en una sola l\u00ednea de c\u00f3digo. Util\u00edzalo con criterio para obtener un c\u00f3digo conciso y legible.<\/p>","protected":false},"template":"","meta":{"_acf_changed":false,"inline_featured_image":false,"footnotes":""},"class_list":["post-6667","dictionary","type-dictionary","status-publish","hentry"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v27.3 (Yoast SEO v27.3) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>Ternary Operator - The Codest<\/title>\n<meta name=\"description\" content=\"Learn about the ternary operator, a shorthand way of writing if-else statements in a single line of code. Use it judiciously for concise, readable code.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/thecodest.co\/es\/diccionario\/operador-ternario\/\" \/>\n<meta property=\"og:locale\" content=\"es_ES\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Ternary Operator\" \/>\n<meta property=\"og:description\" content=\"Learn about the ternary operator, a shorthand way of writing if-else statements in a single line of code. Use it judiciously for concise, readable code.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/thecodest.co\/es\/diccionario\/operador-ternario\/\" \/>\n<meta property=\"og:site_name\" content=\"The Codest\" \/>\n<meta property=\"article:modified_time\" content=\"2026-04-13T11:50:50+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/thecodest.co\/app\/uploads\/2024\/06\/cover-image.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"675\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/thecodest.co\\\/dictionary\\\/ternary-operator\\\/\",\"url\":\"https:\\\/\\\/thecodest.co\\\/dictionary\\\/ternary-operator\\\/\",\"name\":\"Ternary Operator - The Codest\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/thecodest.co\\\/#website\"},\"datePublished\":\"2026-04-13T11:50:46+00:00\",\"dateModified\":\"2026-04-13T11:50:50+00:00\",\"description\":\"Learn about the ternary operator, a shorthand way of writing if-else statements in a single line of code. Use it judiciously for concise, readable code.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/thecodest.co\\\/dictionary\\\/ternary-operator\\\/#breadcrumb\"},\"inLanguage\":\"es-ES\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/thecodest.co\\\/dictionary\\\/ternary-operator\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/thecodest.co\\\/dictionary\\\/ternary-operator\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/thecodest.co\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Dictionary\",\"item\":\"https:\\\/\\\/thecodest.co\\\/sv\\\/ordbok\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Ternary Operator\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/thecodest.co\\\/#website\",\"url\":\"https:\\\/\\\/thecodest.co\\\/\",\"name\":\"The Codest\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\\\/\\\/thecodest.co\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/thecodest.co\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"es-ES\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/thecodest.co\\\/#organization\",\"name\":\"The Codest\",\"url\":\"https:\\\/\\\/thecodest.co\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"es-ES\",\"@id\":\"https:\\\/\\\/thecodest.co\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/thecodest.co\\\/app\\\/uploads\\\/2024\\\/03\\\/thecodest-logo.svg\",\"contentUrl\":\"https:\\\/\\\/thecodest.co\\\/app\\\/uploads\\\/2024\\\/03\\\/thecodest-logo.svg\",\"width\":144,\"height\":36,\"caption\":\"The Codest\"},\"image\":{\"@id\":\"https:\\\/\\\/thecodest.co\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/pl.linkedin.com\\\/company\\\/codest\",\"https:\\\/\\\/clutch.co\\\/profile\\\/codest\"]}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Operador ternario - The Codest","description":"Conozca el operador ternario, una forma abreviada de escribir sentencias if-else en una sola l\u00ednea de c\u00f3digo. Util\u00edzalo con criterio para obtener un c\u00f3digo conciso y legible.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/thecodest.co\/es\/diccionario\/operador-ternario\/","og_locale":"es_ES","og_type":"article","og_title":"Ternary Operator","og_description":"Learn about the ternary operator, a shorthand way of writing if-else statements in a single line of code. Use it judiciously for concise, readable code.","og_url":"https:\/\/thecodest.co\/es\/diccionario\/operador-ternario\/","og_site_name":"The Codest","article_modified_time":"2026-04-13T11:50:50+00:00","og_image":[{"width":1200,"height":675,"url":"https:\/\/thecodest.co\/app\/uploads\/2024\/06\/cover-image.png","type":"image\/png"}],"twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/thecodest.co\/dictionary\/ternary-operator\/","url":"https:\/\/thecodest.co\/dictionary\/ternary-operator\/","name":"Operador ternario - The Codest","isPartOf":{"@id":"https:\/\/thecodest.co\/#website"},"datePublished":"2026-04-13T11:50:46+00:00","dateModified":"2026-04-13T11:50:50+00:00","description":"Conozca el operador ternario, una forma abreviada de escribir sentencias if-else en una sola l\u00ednea de c\u00f3digo. Util\u00edzalo con criterio para obtener un c\u00f3digo conciso y legible.","breadcrumb":{"@id":"https:\/\/thecodest.co\/dictionary\/ternary-operator\/#breadcrumb"},"inLanguage":"es-ES","potentialAction":[{"@type":"ReadAction","target":["https:\/\/thecodest.co\/dictionary\/ternary-operator\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/thecodest.co\/dictionary\/ternary-operator\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/thecodest.co\/"},{"@type":"ListItem","position":2,"name":"Dictionary","item":"https:\/\/thecodest.co\/sv\/ordbok\/"},{"@type":"ListItem","position":3,"name":"Ternary Operator"}]},{"@type":"WebSite","@id":"https:\/\/thecodest.co\/#website","url":"https:\/\/thecodest.co\/","name":"The Codest","description":"","publisher":{"@id":"https:\/\/thecodest.co\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/thecodest.co\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"es-ES"},{"@type":"Organization","@id":"https:\/\/thecodest.co\/#organization","name":"The Codest","url":"https:\/\/thecodest.co\/","logo":{"@type":"ImageObject","inLanguage":"es-ES","@id":"https:\/\/thecodest.co\/#\/schema\/logo\/image\/","url":"https:\/\/thecodest.co\/app\/uploads\/2024\/03\/thecodest-logo.svg","contentUrl":"https:\/\/thecodest.co\/app\/uploads\/2024\/03\/thecodest-logo.svg","width":144,"height":36,"caption":"The Codest"},"image":{"@id":"https:\/\/thecodest.co\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/pl.linkedin.com\/company\/codest","https:\/\/clutch.co\/profile\/codest"]}]}},"_links":{"self":[{"href":"https:\/\/thecodest.co\/es\/wp-json\/wp\/v2\/dictionary\/6667","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/thecodest.co\/es\/wp-json\/wp\/v2\/dictionary"}],"about":[{"href":"https:\/\/thecodest.co\/es\/wp-json\/wp\/v2\/types\/dictionary"}],"version-history":[{"count":1,"href":"https:\/\/thecodest.co\/es\/wp-json\/wp\/v2\/dictionary\/6667\/revisions"}],"predecessor-version":[{"id":10932,"href":"https:\/\/thecodest.co\/es\/wp-json\/wp\/v2\/dictionary\/6667\/revisions\/10932"}],"wp:attachment":[{"href":"https:\/\/thecodest.co\/es\/wp-json\/wp\/v2\/media?parent=6667"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}