snipt

Ctrl+h for KB shortcuts

JavaScript

Javascript Ternary Example

1
2
3
4
var colour, favcol;
colour = window.location.search.substring(1);
favcol = ('green' === colour) ? 'is green.' : 'is not green.';
document.getElementById('ex').innerHTML = colour + favcol;
https://snipt.net/embed/5f448fbee2199fe16ee276ec71627571/
/raw/5f448fbee2199fe16ee276ec71627571/
5f448fbee2199fe16ee276ec71627571
js
JavaScript
4
2019-07-14T01:20:52
True
False
False
/api/public/snipt/45253/
javascript-ternary-example
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a> <a href="#L-3">3</a> <a href="#L-4">4</a></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a><span class="kd">var</span> <span class="nx">colour</span><span class="p">,</span> <span class="nx">favcol</span><span class="p">;</span> </span><span id="L-2"><a name="L-2"></a><span class="nx">colour</span> <span class="o">=</span> <span class="nb">window</span><span class="p">.</span><span class="nx">location</span><span class="p">.</span><span class="nx">search</span><span class="p">.</span><span class="nx">substring</span><span class="p">(</span><span class="mi">1</span><span class="p">);</span> </span><span id="L-3"><a name="L-3"></a><span class="nx">favcol</span> <span class="o">=</span> <span class="p">(</span><span class="s1">&#39;green&#39;</span> <span class="o">===</span> <span class="nx">colour</span><span class="p">)</span> <span class="o">?</span> <span class="s1">&#39;is green.&#39;</span> <span class="o">:</span> <span class="s1">&#39;is not green.&#39;</span><span class="p">;</span> </span><span id="L-4"><a name="L-4"></a><span class="nb">document</span><span class="p">.</span><span class="nx">getElementById</span><span class="p">(</span><span class="s1">&#39;ex&#39;</span><span class="p">).</span><span class="nx">innerHTML</span> <span class="o">=</span> <span class="nx">colour</span> <span class="o">+</span> <span class="nx">favcol</span><span class="p">;</span> </span></pre></div> </td></tr></table>
javascript, ternary