{"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":"operateur-ternaire","status":"publish","type":"dictionary","link":"https:\/\/thecodest.co\/fr\/dictionary\/ternary-operator\/","title":{"rendered":"Op\u00e9rateur ternaire"},"content":{"rendered":"<h1>Op\u00e9rateur ternaire<\/h1>\n<p>L'op\u00e9rateur ternaire, \u00e9galement connu sous le nom d'op\u00e9rateur conditionnel, est une mani\u00e8re abr\u00e9g\u00e9e d'\u00e9crire une instruction if-else en une seule ligne de texte. <a href=\"https:\/\/thecodest.co\/fr\/dictionary\/what-is-code-refactoring\/\">code<\/a>. Il prend trois op\u00e9randes : une condition, une valeur \u00e0 retourner si la condition est vraie, et une valeur \u00e0 retourner si la condition est fausse. La syntaxe de l'op\u00e9rateur ternaire est la suivante :<\/p>\n<p><code>condition ? valeur_si_true : valeur_si_false<\/code><\/p>\n<p>La condition est \u00e9valu\u00e9e en premier. Si elle est vraie, l'expression value_if_true est renvoy\u00e9e. Si elle est fausse, l'expression value_if_false est renvoy\u00e9e. L'op\u00e9rateur ternaire est souvent utilis\u00e9 comme une alternative plus concise \u00e0 l'instruction if-else dans les situations o\u00f9 la condition est simple et o\u00f9 le code r\u00e9sultant est facile \u00e0 lire.<\/p>\n<p>Par exemple, consid\u00e9rons l'instruction if-else suivante :<\/p>\n<p><code>if (x &gt; 0) { y = x ; } else { y = -x ; }<\/code><\/p>\n<p>Ceci peut \u00eatre r\u00e9\u00e9crit en utilisant l'op\u00e9rateur ternaire comme suit :<\/p>\n<p><code>y = (x &gt; 0) ? x : -x ;<\/code><\/p>\n<p>L'op\u00e9rateur ternaire peut \u00e9galement \u00eatre imbriqu\u00e9 pour traiter des conditions plus complexes. Par exemple, l'op\u00e9rateur ternaire peut \u00eatre embo\u00eet\u00e9 pour traiter des conditions plus complexes :<\/p>\n<p><code>r\u00e9sultat = (x &gt; 0) ? \"Positif\" : (x &lt; 0) ? &quot;N\u00e9gatif&quot; : &quot;Z\u00e9ro&quot; ;<\/code><\/p>\n<p>Ce code v\u00e9rifie si x est sup\u00e9rieur \u00e0 0. Si c'est le cas, la valeur \"Positive\" est renvoy\u00e9e. Dans le cas contraire, le code v\u00e9rifie si x est inf\u00e9rieur \u00e0 0. Si c'est le cas, la valeur \"N\u00e9gatif\" est renvoy\u00e9e. Si x n'est ni sup\u00e9rieur ni inf\u00e9rieur \u00e0 0, la valeur \"Z\u00e9ro\" est renvoy\u00e9e.<\/p>\n<p>Bien que l'op\u00e9rateur ternaire puisse rendre le code plus concis et plus facile \u00e0 lire dans certains cas, il peut \u00e9galement rendre le code plus difficile \u00e0 comprendre s'il est utilis\u00e9 de mani\u00e8re excessive ou inappropri\u00e9e. Il est important d'utiliser l'op\u00e9rateur ternaire de mani\u00e8re judicieuse et de privil\u00e9gier la clart\u00e9 et la maintenabilit\u00e9 du code par rapport \u00e0 la bri\u00e8vet\u00e9.<\/p>","protected":false},"excerpt":{"rendered":"<p>D\u00e9couvrez l'op\u00e9rateur ternaire, une mani\u00e8re abr\u00e9g\u00e9e d'\u00e9crire des instructions if-else dans une seule ligne de code. Utilisez-le judicieusement pour obtenir un code concis et lisible.<\/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\/fr\/dictionnaire\/operateur-ternaire\/\" \/>\n<meta property=\"og:locale\" content=\"fr_FR\" \/>\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\/fr\/dictionnaire\/operateur-ternaire\/\" \/>\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\":\"fr-FR\",\"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\":\"fr-FR\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/thecodest.co\\\/#organization\",\"name\":\"The Codest\",\"url\":\"https:\\\/\\\/thecodest.co\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"fr-FR\",\"@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":"Op\u00e9rateur ternaire - The Codest","description":"D\u00e9couvrez l'op\u00e9rateur ternaire, une mani\u00e8re abr\u00e9g\u00e9e d'\u00e9crire des instructions if-else dans une seule ligne de code. Utilisez-le judicieusement pour obtenir un code concis et lisible.","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\/fr\/dictionnaire\/operateur-ternaire\/","og_locale":"fr_FR","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\/fr\/dictionnaire\/operateur-ternaire\/","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":"Op\u00e9rateur ternaire - 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":"D\u00e9couvrez l'op\u00e9rateur ternaire, une mani\u00e8re abr\u00e9g\u00e9e d'\u00e9crire des instructions if-else dans une seule ligne de code. Utilisez-le judicieusement pour obtenir un code concis et lisible.","breadcrumb":{"@id":"https:\/\/thecodest.co\/dictionary\/ternary-operator\/#breadcrumb"},"inLanguage":"fr-FR","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":"fr-FR"},{"@type":"Organization","@id":"https:\/\/thecodest.co\/#organization","name":"The Codest","url":"https:\/\/thecodest.co\/","logo":{"@type":"ImageObject","inLanguage":"fr-FR","@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\/fr\/wp-json\/wp\/v2\/dictionary\/6667","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/thecodest.co\/fr\/wp-json\/wp\/v2\/dictionary"}],"about":[{"href":"https:\/\/thecodest.co\/fr\/wp-json\/wp\/v2\/types\/dictionary"}],"version-history":[{"count":1,"href":"https:\/\/thecodest.co\/fr\/wp-json\/wp\/v2\/dictionary\/6667\/revisions"}],"predecessor-version":[{"id":10932,"href":"https:\/\/thecodest.co\/fr\/wp-json\/wp\/v2\/dictionary\/6667\/revisions\/10932"}],"wp:attachment":[{"href":"https:\/\/thecodest.co\/fr\/wp-json\/wp\/v2\/media?parent=6667"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}