{"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":"ternarni-operator","status":"publish","type":"dictionary","link":"https:\/\/thecodest.co\/cs\/dictionary\/ternary-operator\/","title":{"rendered":"Tern\u00e1rn\u00ed oper\u00e1tor"},"content":{"rendered":"<h1>Tern\u00e1rn\u00ed oper\u00e1tor<\/h1>\n<p>T\u0159\u00edslo\u017ekov\u00fd oper\u00e1tor, zn\u00e1m\u00fd tak\u00e9 jako podm\u00edn\u011bn\u00fd oper\u00e1tor, je zkr\u00e1cen\u00fd zp\u016fsob z\u00e1pisu p\u0159\u00edkazu if-else na jedin\u00e9m \u0159\u00e1dku. <a href=\"https:\/\/thecodest.co\/cs\/dictionary\/what-is-code-refactoring\/\">k\u00f3d<\/a>. P\u0159ij\u00edm\u00e1 t\u0159i operandy: podm\u00ednku, hodnotu, kter\u00e1 se m\u00e1 vr\u00e1tit, pokud je podm\u00ednka pravdiv\u00e1, a hodnotu, kter\u00e1 se m\u00e1 vr\u00e1tit, pokud je podm\u00ednka nepravdiv\u00e1. Syntaxe tern\u00e1rn\u00edho oper\u00e1toru je n\u00e1sleduj\u00edc\u00ed:<\/p>\n<p><code>condition ? value_if_true : value_if_false<\/code><\/p>\n<p>Nejprve se vyhodnot\u00ed podm\u00ednka. Pokud je pravdiv\u00e1, je vr\u00e1cen v\u00fdraz value_if_true. Pokud je nepravdiv\u00e1, je vr\u00e1cen v\u00fdraz value_if_false. Tern\u00e1rn\u00ed oper\u00e1tor se \u010dasto pou\u017e\u00edv\u00e1 jako stru\u010dn\u011bj\u0161\u00ed alternativa k p\u0159\u00edkazu if-else v situac\u00edch, kdy je podm\u00ednka jednoduch\u00e1 a v\u00fdsledn\u00fd k\u00f3d je snadno \u010diteln\u00fd.<\/p>\n<p>Vezm\u011bme si nap\u0159\u00edklad n\u00e1sleduj\u00edc\u00ed p\u0159\u00edkaz if-else:<\/p>\n<p><code>if (x &gt; 0) { y = x; } else { y = -x; }<\/code><\/p>\n<p>To lze p\u0159epsat pomoc\u00ed tern\u00e1rn\u00edho oper\u00e1toru takto:<\/p>\n<p><code>y = (x &gt; 0) ? x : -x;<\/code><\/p>\n<p>Tern\u00e1rn\u00ed oper\u00e1tor lze tak\u00e9 vno\u0159it a zpracovat tak slo\u017eit\u011bj\u0161\u00ed podm\u00ednky. Nap\u0159\u00edklad:<\/p>\n<p><code>result = (x &gt; 0) ? \"Kladn\u00fd\" : (x &lt; 0) ? &quot;Z\u00e1porn\u00fd&quot; : &quot;Nula&quot;;<\/code><\/p>\n<p>Tento k\u00f3d zkontroluje, zda je x v\u011bt\u0161\u00ed ne\u017e 0. Pokud ano, vr\u00e1t\u00ed se hodnota \"Kladn\u00e1\". Pokud ne, k\u00f3d zkontroluje, zda je x men\u0161\u00ed ne\u017e 0. Pokud je, vr\u00e1t\u00ed se hodnota \"Z\u00e1porn\u00e1\". Pokud x nen\u00ed ani v\u011bt\u0161\u00ed, ani men\u0161\u00ed ne\u017e 0, je vr\u00e1cena hodnota \"nula\".<\/p>\n<p>Zat\u00edmco tern\u00e1rn\u00ed oper\u00e1tor m\u016f\u017ee v n\u011bkter\u00fdch p\u0159\u00edpadech u\u010dinit k\u00f3d stru\u010dn\u011bj\u0161\u00edm a p\u0159ehledn\u011bj\u0161\u00edm, p\u0159i nadm\u011brn\u00e9m nebo nevhodn\u00e9m pou\u017eit\u00ed m\u016f\u017ee tak\u00e9 zt\u00ed\u017eit jeho pochopen\u00ed. Je d\u016fle\u017eit\u00e9 pou\u017e\u00edvat tern\u00e1rn\u00ed oper\u00e1tor uv\u00e1\u017eliv\u011b a up\u0159ednostnit p\u0159ehlednost a udr\u017eovatelnost k\u00f3du p\u0159ed stru\u010dnost\u00ed.<\/p>","protected":false},"excerpt":{"rendered":"<p>Seznamte se s tern\u00e1rn\u00edm oper\u00e1torem, zkr\u00e1cen\u00fdm zp\u016fsobem z\u00e1pisu p\u0159\u00edkaz\u016f if-else na jednom \u0159\u00e1dku k\u00f3du. Pou\u017e\u00edvejte jej s rozmyslem pro stru\u010dn\u00fd a \u010diteln\u00fd k\u00f3d.<\/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\/cs\/slovnik\/ternarni-operator\/\" \/>\n<meta property=\"og:locale\" content=\"cs_CZ\" \/>\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\/cs\/slovnik\/ternarni-operator\/\" \/>\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\":\"cs-CZ\",\"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\":\"cs-CZ\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/thecodest.co\\\/#organization\",\"name\":\"The Codest\",\"url\":\"https:\\\/\\\/thecodest.co\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"cs-CZ\",\"@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":"Tern\u00e1rn\u00ed oper\u00e1tor - The Codest","description":"Seznamte se s tern\u00e1rn\u00edm oper\u00e1torem, zkr\u00e1cen\u00fdm zp\u016fsobem z\u00e1pisu p\u0159\u00edkaz\u016f if-else na jednom \u0159\u00e1dku k\u00f3du. Pou\u017e\u00edvejte jej s rozmyslem pro stru\u010dn\u00fd a \u010diteln\u00fd k\u00f3d.","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\/cs\/slovnik\/ternarni-operator\/","og_locale":"cs_CZ","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\/cs\/slovnik\/ternarni-operator\/","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":"Tern\u00e1rn\u00ed oper\u00e1tor - 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":"Seznamte se s tern\u00e1rn\u00edm oper\u00e1torem, zkr\u00e1cen\u00fdm zp\u016fsobem z\u00e1pisu p\u0159\u00edkaz\u016f if-else na jednom \u0159\u00e1dku k\u00f3du. Pou\u017e\u00edvejte jej s rozmyslem pro stru\u010dn\u00fd a \u010diteln\u00fd k\u00f3d.","breadcrumb":{"@id":"https:\/\/thecodest.co\/dictionary\/ternary-operator\/#breadcrumb"},"inLanguage":"cs-CZ","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":"cs-CZ"},{"@type":"Organization","@id":"https:\/\/thecodest.co\/#organization","name":"The Codest","url":"https:\/\/thecodest.co\/","logo":{"@type":"ImageObject","inLanguage":"cs-CZ","@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\/cs\/wp-json\/wp\/v2\/dictionary\/6667","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/thecodest.co\/cs\/wp-json\/wp\/v2\/dictionary"}],"about":[{"href":"https:\/\/thecodest.co\/cs\/wp-json\/wp\/v2\/types\/dictionary"}],"version-history":[{"count":1,"href":"https:\/\/thecodest.co\/cs\/wp-json\/wp\/v2\/dictionary\/6667\/revisions"}],"predecessor-version":[{"id":10932,"href":"https:\/\/thecodest.co\/cs\/wp-json\/wp\/v2\/dictionary\/6667\/revisions\/10932"}],"wp:attachment":[{"href":"https:\/\/thecodest.co\/cs\/wp-json\/wp\/v2\/media?parent=6667"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}