{"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":"triskarsais-operators","status":"publish","type":"dictionary","link":"https:\/\/thecodest.co\/lv\/dictionary\/ternary-operator\/","title":{"rendered":"Tern\u0101rais operators"},"content":{"rendered":"<h1>Tern\u0101rais operators<\/h1>\n<p>Tr\u012bsk\u0101r\u0161ais operators, kas paz\u012bstams ar\u012b k\u0101 nosac\u012bjuma operators, ir sa\u012bsin\u0101ts veids, k\u0101 rakst\u012bt if-else pazi\u0146ojumu vien\u0101 rind\u0101. <a href=\"https:\/\/thecodest.co\/lv\/dictionary\/what-is-code-refactoring\/\">kods<\/a>. Tam ir tr\u012bs operandi: nosac\u012bjums, v\u0113rt\u012bba, kas j\u0101atgrie\u017e, ja nosac\u012bjums ir patiess, un v\u0113rt\u012bba, kas j\u0101atgrie\u017e, ja nosac\u012bjums ir nepatiess. Tr\u012bsk\u0101r\u0161\u0101 operatora sintakse ir \u0161\u0101da:<\/p>\n<p><code>nosac\u012bjums ? value_if_true : value_if_false<\/code><\/p>\n<p>Vispirms tiek nov\u0113rt\u0113ts nosac\u012bjums. Ja tas ir patiess, tiek atgriezta izteiksme value_if_true. Ja nosac\u012bjums ir nepatiess, tiek atgriezta izteiksme value_if_false. Tr\u012bsk\u0101r\u0161o operatoru bie\u017ei izmanto k\u0101 \u012bs\u0101ku alternat\u012bvu if-else izteikumam situ\u0101cij\u0101s, kad nosac\u012bjums ir vienk\u0101r\u0161s un ieg\u016btais kods ir viegli las\u0101ms.<\/p>\n<p>Piem\u0113ram, apl\u016bkojiet \u0161\u0101du if-else pazi\u0146ojumu:<\/p>\n<p><code>ja (x &gt; 0) { y = x; } cit\u0101di { y = -x; }<\/code><\/p>\n<p>To var p\u0101rrakst\u012bt, izmantojot tr\u012bsk\u0101r\u0161o operatoru, \u0161\u0101di:<\/p>\n<p><code>y = (x &gt; 0) ? x : -x;<\/code><\/p>\n<p>Tr\u012bsk\u0101r\u0161o operatoru var ar\u012b ievietot, lai apstr\u0101d\u0101tu sare\u017e\u0123\u012bt\u0101kus nosac\u012bjumus. Piem\u0113ram:<\/p>\n<p><code>rezult\u0101ts = (x &gt; 0) ? \"Pozit\u012bvs\" : (x &lt; 0) ? &quot;negat\u012bvs&quot; : &quot;nulle&quot;;<\/code><\/p>\n<p>\u0160is kods p\u0101rbauda, vai x ir liel\u0101ks par 0. Ja ir, tiek atgriezta v\u0113rt\u012bba \"Pozit\u012bvs\". Ja n\u0113, kods p\u0101rbauda, vai x ir maz\u0101ks par 0. Ja ir, tad tiek atgriezta v\u0113rt\u012bba \"Negat\u012bvs\". Ja x nav ne liel\u0101ks, ne maz\u0101ks par 0, tiek atgriezta v\u0113rt\u012bba \"Nulle\".<\/p>\n<p>Lai gan da\u017eos gad\u012bjumos tr\u012bsk\u0101r\u0161ais operators var padar\u012bt kodu kodeksu kodol\u012bg\u0101ku un viegl\u0101k las\u0101mu, tas var ar\u012b apgr\u016btin\u0101t t\u0101 izpratni, ja tiek lietots p\u0101rm\u0113r\u012bgi vai neatbilsto\u0161i. Ir svar\u012bgi izmantot tr\u012bsk\u0101r\u0161o operatoru apdom\u012bgi un par priorit\u0101ti izvirz\u012bt koda skaidr\u012bbu un uztur\u0113jam\u012bbu, nevis \u012bsumu.<\/p>","protected":false},"excerpt":{"rendered":"<p>Uzziniet vair\u0101k par tr\u012bsk\u0101r\u0161o operatoru, kas ir sa\u012bsin\u0101ts veids, k\u0101 rakst\u012bt if-else pazi\u0146ojumus vien\u0101 koda rind\u0101. Izmantojiet to sapr\u0101t\u012bgi, lai ieg\u016btu kod\u0101 kodus, kas ir kod\u0113ti un saprotami.<\/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\/lv\/vardnica\/triskarsais-operators\/\" \/>\n<meta property=\"og:locale\" content=\"lv_LV\" \/>\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\/lv\/vardnica\/triskarsais-operators\/\" \/>\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\":\"lv\",\"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\":\"lv\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/thecodest.co\\\/#organization\",\"name\":\"The Codest\",\"url\":\"https:\\\/\\\/thecodest.co\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"lv\",\"@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\u0101rais operators - The Codest","description":"Uzziniet vair\u0101k par tr\u012bsk\u0101r\u0161o operatoru, kas ir sa\u012bsin\u0101ts veids, k\u0101 rakst\u012bt if-else pazi\u0146ojumus vien\u0101 koda rind\u0101. Izmantojiet to sapr\u0101t\u012bgi, lai ieg\u016btu kod\u0101 kodus, kas ir kod\u0113ti un saprotami.","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\/lv\/vardnica\/triskarsais-operators\/","og_locale":"lv_LV","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\/lv\/vardnica\/triskarsais-operators\/","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\u0101rais operators - 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":"Uzziniet vair\u0101k par tr\u012bsk\u0101r\u0161o operatoru, kas ir sa\u012bsin\u0101ts veids, k\u0101 rakst\u012bt if-else pazi\u0146ojumus vien\u0101 koda rind\u0101. Izmantojiet to sapr\u0101t\u012bgi, lai ieg\u016btu kod\u0101 kodus, kas ir kod\u0113ti un saprotami.","breadcrumb":{"@id":"https:\/\/thecodest.co\/dictionary\/ternary-operator\/#breadcrumb"},"inLanguage":"lv","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":"lv"},{"@type":"Organization","@id":"https:\/\/thecodest.co\/#organization","name":"The Codest","url":"https:\/\/thecodest.co\/","logo":{"@type":"ImageObject","inLanguage":"lv","@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\/lv\/wp-json\/wp\/v2\/dictionary\/6667","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/thecodest.co\/lv\/wp-json\/wp\/v2\/dictionary"}],"about":[{"href":"https:\/\/thecodest.co\/lv\/wp-json\/wp\/v2\/types\/dictionary"}],"version-history":[{"count":1,"href":"https:\/\/thecodest.co\/lv\/wp-json\/wp\/v2\/dictionary\/6667\/revisions"}],"predecessor-version":[{"id":10932,"href":"https:\/\/thecodest.co\/lv\/wp-json\/wp\/v2\/dictionary\/6667\/revisions\/10932"}],"wp:attachment":[{"href":"https:\/\/thecodest.co\/lv\/wp-json\/wp\/v2\/media?parent=6667"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}