{"id":9345,"date":"2021-01-05T14:34:40","date_gmt":"2021-01-05T19:34:40","guid":{"rendered":"http:\/\/local.brightwhiz\/?p=9345"},"modified":"2021-12-04T06:42:47","modified_gmt":"2021-12-04T06:42:47","slug":"javascript-ternary-operator-examples","status":"publish","type":"post","link":"http:\/\/local.brightwhiz\/javascript-ternary-operator-examples\/","title":{"rendered":"How to Use The JavaScript Ternary Operator With Examples"},"content":{"rendered":"\n

The JavaScript ternary operator also called the conditional operator or Question mark operator is an operator that takes three operands. It is the only JavaScript operator that takes three operands.<\/p>\n\n\n\n

It consists of a condition followed by a question mark (?), then an expression to execute if the condition is true followed by a colon (:). Finally, the expression to execute if the condition is false comes thereafter. This operator is frequently used as a shortcut for the if statement.<\/p>\n\n\n\n

The following is the syntax of the JavaScript<\/a> ternary operator:<\/p>\n\n\n\n

condition ? value if true : value if false<\/code><\/p>\n\n\n\n

Let’s take a look at a simple example:<\/p>\n\n\n\n

var age = 18;\nvar transport = (age >= 16) ? "Drive to school" : "Take the bus";\n\nconsole.log( transport ); \/\/ "Drive to school"<\/code><\/pre>\n\n\n\n

<\/p>\n\n\n\n

Using JavaScript Ternary Operator Conditional Chains<\/h2>\n\n\n\n

The ternary operator is right-associative, which means it can be “chained”. This is similar to an if \u2026 else<\/em><\/strong> chain as compared below:<\/p>\n\n\n\n

function example(\u2026) {\n    return condition1 ? value1\n         : condition2 ? value2\n         : condition3 ? value3\n         : value4;\n}<\/code><\/pre>\n\n\n\n

<\/p>\n\n\n\n

The above is equivalent to this:<\/p>\n\n\n\n

function example(\u2026) {\nif (condition1) { return value1; }\nelse if (condition2) { return value2; }\nelse if (condition3) { return value3; }\nelse { return value4; }\n}<\/code><\/pre>\n\n\n\n

<\/p>\n\n\n\n

Ref: https:\/\/tc39.es\/ecma262\/#sec-conditional-operator<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"

The JavaScript ternary operator also called the conditional operator or Question mark operator is an operator that takes three operands. It is the only JavaScript operator that takes three operands….<\/p>\n","protected":false},"author":1,"featured_media":9346,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,23,9,27,16],"tags":[328,433,449,471,635,638],"yoast_head":"\nHow to Use The JavaScript Ternary Operator With Examples<\/title>\n<meta name=\"description\" content=\"In this quick guide we discuss the JavaScript ternary operator with examples which include the conditional operator chains\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"http:\/\/local.brightwhiz\/javascript-ternary-operator-examples\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to Use The JavaScript Ternary Operator With Examples\" \/>\n<meta property=\"og:description\" content=\"In this quick guide we discuss the JavaScript ternary operator with examples which include the conditional operator chains\" \/>\n<meta property=\"og:url\" content=\"http:\/\/local.brightwhiz\/javascript-ternary-operator-examples\/\" \/>\n<meta property=\"og:site_name\" content=\"Brightwhiz.com\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/brightwhiz\/\" \/>\n<meta property=\"article:published_time\" content=\"2021-01-05T19:34:40+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-12-04T06:42:47+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/local.brightwhiz\/wp-content\/uploads\/2021\/01\/javascript-ternary-operator.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"630\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Michael Bright\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@brightwhizmag\" \/>\n<meta name=\"twitter:site\" content=\"@brightwhizmag\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Michael Bright\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"http:\/\/local.brightwhiz\/javascript-ternary-operator-examples\/#article\",\"isPartOf\":{\"@id\":\"http:\/\/local.brightwhiz\/javascript-ternary-operator-examples\/\"},\"author\":{\"name\":\"Michael Bright\",\"@id\":\"http:\/\/local.brightwhiz\/#\/schema\/person\/81f0f3126f13834ae2e7f381b3028e32\"},\"headline\":\"How to Use The JavaScript Ternary Operator With Examples\",\"datePublished\":\"2021-01-05T19:34:40+00:00\",\"dateModified\":\"2021-12-04T06:42:47+00:00\",\"mainEntityOfPage\":{\"@id\":\"http:\/\/local.brightwhiz\/javascript-ternary-operator-examples\/\"},\"wordCount\":146,\"publisher\":{\"@id\":\"http:\/\/local.brightwhiz\/#organization\"},\"image\":{\"@id\":\"http:\/\/local.brightwhiz\/javascript-ternary-operator-examples\/#primaryimage\"},\"thumbnailUrl\":\"http:\/\/local.brightwhiz\/wp-content\/uploads\/2021\/01\/javascript-ternary-operator.jpg\",\"keywords\":[\"JavaScript\",\"Optimization\",\"Performance\",\"Programming\",\"Web\",\"Web Development\"],\"articleSection\":[\"Articles\",\"Guides\",\"How To\",\"Programming\",\"Technology\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"http:\/\/local.brightwhiz\/javascript-ternary-operator-examples\/\",\"url\":\"http:\/\/local.brightwhiz\/javascript-ternary-operator-examples\/\",\"name\":\"How to Use The JavaScript Ternary Operator With Examples\",\"isPartOf\":{\"@id\":\"http:\/\/local.brightwhiz\/#website\"},\"primaryImageOfPage\":{\"@id\":\"http:\/\/local.brightwhiz\/javascript-ternary-operator-examples\/#primaryimage\"},\"image\":{\"@id\":\"http:\/\/local.brightwhiz\/javascript-ternary-operator-examples\/#primaryimage\"},\"thumbnailUrl\":\"http:\/\/local.brightwhiz\/wp-content\/uploads\/2021\/01\/javascript-ternary-operator.jpg\",\"datePublished\":\"2021-01-05T19:34:40+00:00\",\"dateModified\":\"2021-12-04T06:42:47+00:00\",\"description\":\"In this quick guide we discuss the JavaScript ternary operator with examples which include the conditional operator chains\",\"breadcrumb\":{\"@id\":\"http:\/\/local.brightwhiz\/javascript-ternary-operator-examples\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"http:\/\/local.brightwhiz\/javascript-ternary-operator-examples\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/local.brightwhiz\/javascript-ternary-operator-examples\/#primaryimage\",\"url\":\"http:\/\/local.brightwhiz\/wp-content\/uploads\/2021\/01\/javascript-ternary-operator.jpg\",\"contentUrl\":\"http:\/\/local.brightwhiz\/wp-content\/uploads\/2021\/01\/javascript-ternary-operator.jpg\",\"width\":1200,\"height\":630,\"caption\":\"JavaScript Ternary Operator\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"http:\/\/local.brightwhiz\/javascript-ternary-operator-examples\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"http:\/\/local.brightwhiz\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to Use The JavaScript Ternary Operator With Examples\"}]},{\"@type\":\"WebSite\",\"@id\":\"http:\/\/local.brightwhiz\/#website\",\"url\":\"http:\/\/local.brightwhiz\/\",\"name\":\"Brightwhiz.com\",\"description\":\"Best Tech guides, Tutorials, and News\",\"publisher\":{\"@id\":\"http:\/\/local.brightwhiz\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"http:\/\/local.brightwhiz\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"http:\/\/local.brightwhiz\/#organization\",\"name\":\"Brightwhiz\",\"url\":\"http:\/\/local.brightwhiz\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/local.brightwhiz\/#\/schema\/logo\/image\/\",\"url\":\"http:\/\/local.brightwhiz\/wp-content\/uploads\/2021\/11\/brightwhiz-com-logo-orange.png\",\"contentUrl\":\"http:\/\/local.brightwhiz\/wp-content\/uploads\/2021\/11\/brightwhiz-com-logo-orange.png\",\"width\":706,\"height\":135,\"caption\":\"Brightwhiz\"},\"image\":{\"@id\":\"http:\/\/local.brightwhiz\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/brightwhiz\/\",\"https:\/\/x.com\/brightwhizmag\",\"https:\/\/instagram.com\/bright_whiz\/\",\"https:\/\/www.pinterest.com\/sobbayi\/\",\"https:\/\/www.youtube.com\/channel\/UC6sCdP_d_RiTIM7ErFT-PSQ\"]},{\"@type\":\"Person\",\"@id\":\"http:\/\/local.brightwhiz\/#\/schema\/person\/81f0f3126f13834ae2e7f381b3028e32\",\"name\":\"Michael Bright\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/local.brightwhiz\/#\/schema\/person\/image\/\",\"url\":\"http:\/\/1.gravatar.com\/avatar\/da90485875ff0aafa38fdd494abe87d1?s=96&d=mm&r=g\",\"contentUrl\":\"http:\/\/1.gravatar.com\/avatar\/da90485875ff0aafa38fdd494abe87d1?s=96&d=mm&r=g\",\"caption\":\"Michael Bright\"},\"sameAs\":[\"https:\/\/sobbayi.com\"],\"url\":\"http:\/\/local.brightwhiz\/author\/sobbayiadmin\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to Use The JavaScript Ternary Operator With Examples","description":"In this quick guide we discuss the JavaScript ternary operator with examples which include the conditional operator chains","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":"http:\/\/local.brightwhiz\/javascript-ternary-operator-examples\/","og_locale":"en_US","og_type":"article","og_title":"How to Use The JavaScript Ternary Operator With Examples","og_description":"In this quick guide we discuss the JavaScript ternary operator with examples which include the conditional operator chains","og_url":"http:\/\/local.brightwhiz\/javascript-ternary-operator-examples\/","og_site_name":"Brightwhiz.com","article_publisher":"https:\/\/www.facebook.com\/brightwhiz\/","article_published_time":"2021-01-05T19:34:40+00:00","article_modified_time":"2021-12-04T06:42:47+00:00","og_image":[{"width":1200,"height":630,"url":"http:\/\/local.brightwhiz\/wp-content\/uploads\/2021\/01\/javascript-ternary-operator.jpg","type":"image\/jpeg"}],"author":"Michael Bright","twitter_card":"summary_large_image","twitter_creator":"@brightwhizmag","twitter_site":"@brightwhizmag","twitter_misc":{"Written by":"Michael Bright","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"http:\/\/local.brightwhiz\/javascript-ternary-operator-examples\/#article","isPartOf":{"@id":"http:\/\/local.brightwhiz\/javascript-ternary-operator-examples\/"},"author":{"name":"Michael Bright","@id":"http:\/\/local.brightwhiz\/#\/schema\/person\/81f0f3126f13834ae2e7f381b3028e32"},"headline":"How to Use The JavaScript Ternary Operator With Examples","datePublished":"2021-01-05T19:34:40+00:00","dateModified":"2021-12-04T06:42:47+00:00","mainEntityOfPage":{"@id":"http:\/\/local.brightwhiz\/javascript-ternary-operator-examples\/"},"wordCount":146,"publisher":{"@id":"http:\/\/local.brightwhiz\/#organization"},"image":{"@id":"http:\/\/local.brightwhiz\/javascript-ternary-operator-examples\/#primaryimage"},"thumbnailUrl":"http:\/\/local.brightwhiz\/wp-content\/uploads\/2021\/01\/javascript-ternary-operator.jpg","keywords":["JavaScript","Optimization","Performance","Programming","Web","Web Development"],"articleSection":["Articles","Guides","How To","Programming","Technology"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"http:\/\/local.brightwhiz\/javascript-ternary-operator-examples\/","url":"http:\/\/local.brightwhiz\/javascript-ternary-operator-examples\/","name":"How to Use The JavaScript Ternary Operator With Examples","isPartOf":{"@id":"http:\/\/local.brightwhiz\/#website"},"primaryImageOfPage":{"@id":"http:\/\/local.brightwhiz\/javascript-ternary-operator-examples\/#primaryimage"},"image":{"@id":"http:\/\/local.brightwhiz\/javascript-ternary-operator-examples\/#primaryimage"},"thumbnailUrl":"http:\/\/local.brightwhiz\/wp-content\/uploads\/2021\/01\/javascript-ternary-operator.jpg","datePublished":"2021-01-05T19:34:40+00:00","dateModified":"2021-12-04T06:42:47+00:00","description":"In this quick guide we discuss the JavaScript ternary operator with examples which include the conditional operator chains","breadcrumb":{"@id":"http:\/\/local.brightwhiz\/javascript-ternary-operator-examples\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["http:\/\/local.brightwhiz\/javascript-ternary-operator-examples\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/local.brightwhiz\/javascript-ternary-operator-examples\/#primaryimage","url":"http:\/\/local.brightwhiz\/wp-content\/uploads\/2021\/01\/javascript-ternary-operator.jpg","contentUrl":"http:\/\/local.brightwhiz\/wp-content\/uploads\/2021\/01\/javascript-ternary-operator.jpg","width":1200,"height":630,"caption":"JavaScript Ternary Operator"},{"@type":"BreadcrumbList","@id":"http:\/\/local.brightwhiz\/javascript-ternary-operator-examples\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"http:\/\/local.brightwhiz\/"},{"@type":"ListItem","position":2,"name":"How to Use The JavaScript Ternary Operator With Examples"}]},{"@type":"WebSite","@id":"http:\/\/local.brightwhiz\/#website","url":"http:\/\/local.brightwhiz\/","name":"Brightwhiz.com","description":"Best Tech guides, Tutorials, and News","publisher":{"@id":"http:\/\/local.brightwhiz\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"http:\/\/local.brightwhiz\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Organization","@id":"http:\/\/local.brightwhiz\/#organization","name":"Brightwhiz","url":"http:\/\/local.brightwhiz\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/local.brightwhiz\/#\/schema\/logo\/image\/","url":"http:\/\/local.brightwhiz\/wp-content\/uploads\/2021\/11\/brightwhiz-com-logo-orange.png","contentUrl":"http:\/\/local.brightwhiz\/wp-content\/uploads\/2021\/11\/brightwhiz-com-logo-orange.png","width":706,"height":135,"caption":"Brightwhiz"},"image":{"@id":"http:\/\/local.brightwhiz\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/brightwhiz\/","https:\/\/x.com\/brightwhizmag","https:\/\/instagram.com\/bright_whiz\/","https:\/\/www.pinterest.com\/sobbayi\/","https:\/\/www.youtube.com\/channel\/UC6sCdP_d_RiTIM7ErFT-PSQ"]},{"@type":"Person","@id":"http:\/\/local.brightwhiz\/#\/schema\/person\/81f0f3126f13834ae2e7f381b3028e32","name":"Michael Bright","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/local.brightwhiz\/#\/schema\/person\/image\/","url":"http:\/\/1.gravatar.com\/avatar\/da90485875ff0aafa38fdd494abe87d1?s=96&d=mm&r=g","contentUrl":"http:\/\/1.gravatar.com\/avatar\/da90485875ff0aafa38fdd494abe87d1?s=96&d=mm&r=g","caption":"Michael Bright"},"sameAs":["https:\/\/sobbayi.com"],"url":"http:\/\/local.brightwhiz\/author\/sobbayiadmin\/"}]}},"_links":{"self":[{"href":"http:\/\/local.brightwhiz\/wp-json\/wp\/v2\/posts\/9345"}],"collection":[{"href":"http:\/\/local.brightwhiz\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/local.brightwhiz\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/local.brightwhiz\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/local.brightwhiz\/wp-json\/wp\/v2\/comments?post=9345"}],"version-history":[{"count":0,"href":"http:\/\/local.brightwhiz\/wp-json\/wp\/v2\/posts\/9345\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/local.brightwhiz\/wp-json\/wp\/v2\/media\/9346"}],"wp:attachment":[{"href":"http:\/\/local.brightwhiz\/wp-json\/wp\/v2\/media?parent=9345"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/local.brightwhiz\/wp-json\/wp\/v2\/categories?post=9345"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/local.brightwhiz\/wp-json\/wp\/v2\/tags?post=9345"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}