avatarGerard Sans

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

1079

Abstract

lockquote><h1 id="93aa">Comparisons</h1><p id="23f8">JavaScript evaluates comparisons from left to right. This is for efficiency so as soon as there is a reason to stop evaluating expressions JavaScript will do so.</p><p id="9224">As a general rule <i>OR</i> comparisons will stop as soon as a <i>truthy</i> expression is evaluated. On the other hand, <i>AND</i> comparisons will stop as soon as a <i>falsy</i> expression is evaluated.</p> <figure id="dc74"> <div> <div>

            <iframe class="gist-iframe" src="/gist/gsans/f314ea5d76f97d6ed539.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
          </div>
        </div>
    </figure></iframe></div></div></figure><h1 id="1b8a">OR comparisons ||</h1>
    <figure id="8c09">
        <div>
          <div>
            
            <iframe class="gist-iframe" src="/gist/gsans/e36feaeacb8b1f30a125.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
          </div>
        

Options

</div> </figure></iframe></div></div></figure><h1 id="ab6f">AND comparisons &amp;&amp;</h1> <figure id="7071"> <div> <div>
            <iframe class="gist-iframe" src="/gist/gsans/b68e42ecc958f213bc38.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
          </div>
        </div>
    </figure></iframe></div></div></figure><h1 id="59d0">Not operator !</h1><p id="c9a0">It converts objects to booleans. If the object is <i>falsy</i> it returns <b>true</b> otherwise <b>false</b>.</p>
    <figure id="00ad">
        <div>
          <div>
            
            <iframe class="gist-iframe" src="/gist/gsans/4152103e2a3724aafca6.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
          </div>
        </div>
    </figure></iframe></div></div></figure><figure id="f7a9"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*WMl8WSn2zUlR0w72th6-WA.png"><figcaption></figcaption></figure></article></body>

JavaScript falsy objects and comparisons

Very spooky stuff

In JavaScript you will find yourself scrapping your head around comparisons and perplexed about how expressions evaluate to true or false. This is because JavaScript uses truthy and falsy expressions to evaluate comparisons.

Falsy objects (meet the family)

  • empty string
  • the number zero
  • null
  • NaN
  • the boolean false
  • undefined

Truthy objects are just anything else but the list above. Basically any object that is initialised with any value different from the list above, is truthy.

Take care when using the empty string or 0 as a valid parameter as they might be ignored by some APIs if they precede their code with if (!parameter) return;

Comparisons

JavaScript evaluates comparisons from left to right. This is for efficiency so as soon as there is a reason to stop evaluating expressions JavaScript will do so.

As a general rule OR comparisons will stop as soon as a truthy expression is evaluated. On the other hand, AND comparisons will stop as soon as a falsy expression is evaluated.

OR comparisons ||

AND comparisons &&

Not operator !

It converts objects to booleans. If the object is falsy it returns true otherwise false.

JavaScript
Recommended from ReadMedium