{"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":"thrieykja-rekstraradili","status":"publish","type":"dictionary","link":"https:\/\/thecodest.co\/is\/dictionary\/ternary-operator\/","title":{"rendered":"\u00der\u00edeyki rekstrara\u00f0ila"},"content":{"rendered":"<h1>\u00der\u00edeyki rekstrara\u00f0ila<\/h1>\n<p>\u00der\u00edeykjaa\u00f0ger\u00f0in, einnig k\u00f6llu\u00f0 skilyr\u00f0aa\u00f0ger\u00f0in, er stytting \u00e1 \u00fev\u00ed a\u00f0 skrifa if-else-skilyr\u00f0i \u00e1 einni l\u00ednu af <a href=\"https:\/\/thecodest.co\/is\/dictionary\/what-is-code-refactoring\/\">k\u00f3\u00f0i<\/a>. Hann tekur \u00ferj\u00e1 \u00fe\u00e6tti: skilyr\u00f0i, gildi sem skila \u00e1 a\u00f0 skila ef skilyr\u00f0i\u00f0 er satt og gildi sem skila \u00e1 a\u00f0 skila ef skilyr\u00f0i\u00f0 er falskt. Setningafr\u00e6\u00f0i \u00fer\u00edeykisins er sem h\u00e9r segir:<\/p>\n<p><code>skilyr\u00f0i ? sattgildi_ef_satt : falsgildi_ef_falskt<\/code><\/p>\n<p>Skilyr\u00f0i\u00f0 er fyrst meti\u00f0. Ef \u00fea\u00f0 er satt er gildi_ef_satt skila\u00f0. Ef \u00fea\u00f0 er falskt er gildi_ef_falskt skila\u00f0. \u00der\u00edeyki\u00f0 er oft nota\u00f0 sem styttri valkostur vi\u00f0 if-else-setningu \u00feegar skilyr\u00f0i\u00f0 er einfalt og k\u00f3\u00f0inn sem f\u00e6st au\u00f0velt a\u00f0 lesa.<\/p>\n<p>Til d\u00e6mis skaltu \u00edhuga eftirfarandi if-else-skilyr\u00f0i:<\/p>\n<p><code>ef (x &gt; 0) { y = x; } annars { y = -x; }<\/code><\/p>\n<p>\u00deetta m\u00e1 endurskrifa me\u00f0 \u00fer\u00edli\u00f0uatri\u00f0anum sem:<\/p>\n<p><code>y = (x &gt; 0) ? x : -x;<\/code><\/p>\n<p>\u00der\u00edeykjaa\u00f0ger\u00f0in m\u00e1 einnig hneppa til a\u00f0 takast \u00e1 vi\u00f0 fl\u00f3knari skilyr\u00f0i. Til d\u00e6mis:<\/p>\n<p><code>Ni\u00f0ursta\u00f0a = (x &gt; 0) ? \"J\u00e1kv\u00e6tt\" : (x &lt; 0) ? &quot;Neikv\u00e6tt&quot; : &quot;Null&quot;;<\/code><\/p>\n<p>\u00deessi k\u00f3\u00f0i athugar hvort x s\u00e9 st\u00e6rra en 0. Ef svo er, er gildi\u00f0 \"J\u00e1kv\u00e6tt\" skila\u00f0. Ef ekki, athugar k\u00f3\u00f0inn hvort x s\u00e9 minna en 0. Ef svo er, er gildi\u00f0 \"Neikv\u00e6tt\" skila\u00f0. Ef x er hvorki st\u00e6rra n\u00e9 minna en 0, er gildi\u00f0 \"Null\" skila\u00f0.<\/p>\n<p>\u00de\u00f3 a\u00f0 \u00fer\u00edeyki\u00f0 geti gert k\u00f3\u00f0ann styttri og au\u00f0veldari \u00ed lestri \u00ed sumum tilvikum, getur \u00fea\u00f0 l\u00edka gert k\u00f3\u00f0ann erfi\u00f0ari \u00ed skilningi ef \u00fea\u00f0 er nota\u00f0 of miki\u00f0 e\u00f0a \u00f3vi\u00f0eigandi. Mikilv\u00e6gt er a\u00f0 nota \u00fer\u00edeyki\u00f0 me\u00f0 skynsemi og forgangsra\u00f0a sk\u00fdrleika og vi\u00f0haldanleika k\u00f3\u00f0ans fram yfir styttri k\u00f3\u00f0ager\u00f0.<\/p>","protected":false},"excerpt":{"rendered":"<p>Kynntu \u00fe\u00e9r \u00fer\u00edeyki\u00f0 (ternary operator), sem er stutt lei\u00f0 til a\u00f0 skrifa if-else-skilyr\u00f0i \u00e1 einni k\u00f3\u00f0al\u00ednu. Nota\u00f0u \u00fea\u00f0 skynsamlega til a\u00f0 skrifa hnitmi\u00f0a\u00f0an og l\u00e6silegan k\u00f3\u00f0a.<\/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\/is\/ordabok\/thrieykja-rekstraradili\/\" \/>\n<meta property=\"og:locale\" content=\"is_IS\" \/>\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\/is\/ordabok\/thrieykja-rekstraradili\/\" \/>\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\":\"is\",\"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\":\"is\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/thecodest.co\\\/#organization\",\"name\":\"The Codest\",\"url\":\"https:\\\/\\\/thecodest.co\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"is\",\"@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":"\u00der\u00edeyki rekstrara\u00f0ila - The Codest","description":"Kynntu \u00fe\u00e9r \u00fer\u00edeyki\u00f0 (ternary operator), sem er stutt lei\u00f0 til a\u00f0 skrifa if-else-skilyr\u00f0i \u00e1 einni k\u00f3\u00f0al\u00ednu. Nota\u00f0u \u00fea\u00f0 skynsamlega til a\u00f0 skrifa hnitmi\u00f0a\u00f0an og l\u00e6silegan k\u00f3\u00f0a.","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\/is\/ordabok\/thrieykja-rekstraradili\/","og_locale":"is_IS","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\/is\/ordabok\/thrieykja-rekstraradili\/","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":"\u00der\u00edeyki rekstrara\u00f0ila - 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":"Kynntu \u00fe\u00e9r \u00fer\u00edeyki\u00f0 (ternary operator), sem er stutt lei\u00f0 til a\u00f0 skrifa if-else-skilyr\u00f0i \u00e1 einni k\u00f3\u00f0al\u00ednu. Nota\u00f0u \u00fea\u00f0 skynsamlega til a\u00f0 skrifa hnitmi\u00f0a\u00f0an og l\u00e6silegan k\u00f3\u00f0a.","breadcrumb":{"@id":"https:\/\/thecodest.co\/dictionary\/ternary-operator\/#breadcrumb"},"inLanguage":"is","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":"is"},{"@type":"Organization","@id":"https:\/\/thecodest.co\/#organization","name":"The Codest","url":"https:\/\/thecodest.co\/","logo":{"@type":"ImageObject","inLanguage":"is","@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\/is\/wp-json\/wp\/v2\/dictionary\/6667","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/thecodest.co\/is\/wp-json\/wp\/v2\/dictionary"}],"about":[{"href":"https:\/\/thecodest.co\/is\/wp-json\/wp\/v2\/types\/dictionary"}],"version-history":[{"count":1,"href":"https:\/\/thecodest.co\/is\/wp-json\/wp\/v2\/dictionary\/6667\/revisions"}],"predecessor-version":[{"id":10932,"href":"https:\/\/thecodest.co\/is\/wp-json\/wp\/v2\/dictionary\/6667\/revisions\/10932"}],"wp:attachment":[{"href":"https:\/\/thecodest.co\/is\/wp-json\/wp\/v2\/media?parent=6667"}],"curies":[{"name":"vp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}