{"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":"operator-trojskladnikowy","status":"publish","type":"dictionary","link":"https:\/\/thecodest.co\/pl\/dictionary\/ternary-operator\/","title":{"rendered":"Operator tr\u00f3jsk\u0142adnikowy"},"content":{"rendered":"<h1>Operator tr\u00f3jsk\u0142adnikowy<\/h1>\n<p>Operator tr\u00f3jsk\u0142adnikowy, znany r\u00f3wnie\u017c jako operator warunkowy, to skr\u00f3towy spos\u00f3b zapisu instrukcji if-else w jednym wierszu tekstu <a href=\"https:\/\/thecodest.co\/pl\/dictionary\/what-is-code-refactoring\/\">kod<\/a>. Przyjmuje on trzy operandy: warunek, warto\u015b\u0107 zwracan\u0105, je\u015bli warunek jest prawdziwy, oraz warto\u015b\u0107 zwracan\u0105, je\u015bli warunek jest fa\u0142szywy. Sk\u0142adnia operatora tr\u00f3jargumentowego jest nast\u0119puj\u0105ca:<\/p>\n<p><code>condition ? value_if_true : value_if_false<\/code><\/p>\n<p>Najpierw oceniany jest warunek. Je\u015bli jest prawdziwy, zwracane jest wyra\u017cenie value_if_true. Je\u015bli jest fa\u0142szywy, zwracane jest wyra\u017cenie value_if_false. Operator tr\u00f3jargumentowy jest cz\u0119sto u\u017cywany jako bardziej zwi\u0119z\u0142a alternatywa dla instrukcji if-else w sytuacjach, w kt\u00f3rych warunek jest prosty, a wynikowy kod jest \u0142atwy do odczytania.<\/p>\n<p>Rozwa\u017cmy na przyk\u0142ad nast\u0119puj\u0105c\u0105 instrukcj\u0119 if-else:<\/p>\n<p><code>if (x &gt; 0) { y = x; } else { y = -x; }<\/code><\/p>\n<p>Mo\u017cna to przepisa\u0107 przy u\u017cyciu operatora tr\u00f3jsk\u0142adnikowego jako:<\/p>\n<p><code>y = (x &gt; 0) ? x : -x;<\/code><\/p>\n<p>Operator tr\u00f3jsk\u0142adnikowy mo\u017ce by\u0107 r\u00f3wnie\u017c zagnie\u017cd\u017cony w celu obs\u0142ugi bardziej z\u0142o\u017conych warunk\u00f3w. Na przyk\u0142ad:<\/p>\n<p><code>result = (x &gt; 0) ? \"Pozytywny\" : (x &lt; 0) ? &quot;Ujemny&quot; : &quot;Zero&quot;;<\/code><\/p>\n<p>Ten kod sprawdza, czy x jest wi\u0119ksze od 0. Je\u015bli tak, zwracana jest warto\u015b\u0107 \"Positive\". Je\u015bli nie, kod sprawdza, czy x jest mniejsze od 0. Je\u015bli tak, zwracana jest warto\u015b\u0107 \"Negative\". Je\u015bli x nie jest wi\u0119ksze ani mniejsze od 0, zwracana jest warto\u015b\u0107 \"Zero\".<\/p>\n<p>Chocia\u017c operator tr\u00f3jargumentowy mo\u017ce sprawi\u0107, \u017ce kod b\u0119dzie bardziej zwi\u0119z\u0142y i \u0142atwiejszy do odczytania w niekt\u00f3rych przypadkach, mo\u017ce r\u00f3wnie\u017c utrudni\u0107 zrozumienie kodu, je\u015bli jest u\u017cywany nadmiernie lub niew\u0142a\u015bciwie. Wa\u017cne jest, aby u\u017cywa\u0107 operatora tr\u00f3jargumentowego z rozwag\u0105 i przedk\u0142ada\u0107 przejrzysto\u015b\u0107 kodu nad jego zwi\u0119z\u0142o\u015b\u0107.<\/p>","protected":false},"excerpt":{"rendered":"<p>Dowiedz si\u0119 wi\u0119cej o operatorze tr\u00f3jsk\u0142adnikowym, skr\u00f3conym sposobie pisania instrukcji if-else w jednym wierszu kodu. U\u017cywaj go rozs\u0105dnie, aby uzyska\u0107 zwi\u0119z\u0142y, czytelny kod.<\/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\/pl\/slownik\/operator-trojskladnikowy\/\" \/>\n<meta property=\"og:locale\" content=\"pl_PL\" \/>\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\/pl\/slownik\/operator-trojskladnikowy\/\" \/>\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 minuta\" \/>\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\":\"pl-PL\",\"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\":\"pl-PL\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/thecodest.co\\\/#organization\",\"name\":\"The Codest\",\"url\":\"https:\\\/\\\/thecodest.co\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"pl-PL\",\"@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":"Operator tr\u00f3jsk\u0142adnikowy - The Codest","description":"Dowiedz si\u0119 wi\u0119cej o operatorze tr\u00f3jsk\u0142adnikowym, skr\u00f3conym sposobie pisania instrukcji if-else w jednym wierszu kodu. U\u017cywaj go rozs\u0105dnie, aby uzyska\u0107 zwi\u0119z\u0142y, czytelny kod.","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\/pl\/slownik\/operator-trojskladnikowy\/","og_locale":"pl_PL","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\/pl\/slownik\/operator-trojskladnikowy\/","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 minuta"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/thecodest.co\/dictionary\/ternary-operator\/","url":"https:\/\/thecodest.co\/dictionary\/ternary-operator\/","name":"Operator tr\u00f3jsk\u0142adnikowy - 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":"Dowiedz si\u0119 wi\u0119cej o operatorze tr\u00f3jsk\u0142adnikowym, skr\u00f3conym sposobie pisania instrukcji if-else w jednym wierszu kodu. U\u017cywaj go rozs\u0105dnie, aby uzyska\u0107 zwi\u0119z\u0142y, czytelny kod.","breadcrumb":{"@id":"https:\/\/thecodest.co\/dictionary\/ternary-operator\/#breadcrumb"},"inLanguage":"pl-PL","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":"pl-PL"},{"@type":"Organization","@id":"https:\/\/thecodest.co\/#organization","name":"The Codest","url":"https:\/\/thecodest.co\/","logo":{"@type":"ImageObject","inLanguage":"pl-PL","@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\/pl\/wp-json\/wp\/v2\/dictionary\/6667","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/thecodest.co\/pl\/wp-json\/wp\/v2\/dictionary"}],"about":[{"href":"https:\/\/thecodest.co\/pl\/wp-json\/wp\/v2\/types\/dictionary"}],"version-history":[{"count":1,"href":"https:\/\/thecodest.co\/pl\/wp-json\/wp\/v2\/dictionary\/6667\/revisions"}],"predecessor-version":[{"id":10932,"href":"https:\/\/thecodest.co\/pl\/wp-json\/wp\/v2\/dictionary\/6667\/revisions\/10932"}],"wp:attachment":[{"href":"https:\/\/thecodest.co\/pl\/wp-json\/wp\/v2\/media?parent=6667"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}