Jiadong Wu
  • Portfolio
  • Blog
  • Gallery
  • Lab
  • GitHub
  • About

JavaScript Logic Operators Notes

4/1/2016

0 Comments

 
First of all, we have to know which values are considered false in JavaScript. Basicly, everything in JavaScript is treated as true expect
  • false(boolean value)
  • null/undefined
  • 0/NaN
  • ’’(empty string)

With the above fundamental background, now we can take a look at how lazy JavaScript works.
Rules:
  • JavaScript always tries to return the value itself, no matter it is a String, Object, Number or Boolean, instead of just returning the Boolean number.
  • True && x = x
    If the first value is true(or other values treated as true), then the first value is irrelevant. Thus return the second value since the result is only determined by x.
  • False/null/0/NaN/’’ && x = false/null/0/NaN
    The second value is irrelevant, just give me the first value
  • False/null/0/NaN/’’ || x = x
    Similar to the second rule, in this case, first value never counts.
  • True || x = True
    Note that true can be any value that are treated as true here. The return value will always be the same as the first value.
Exercise

    
0 Comments

    Archives

    April 2020
    January 2019
    November 2018
    April 2018
    March 2018
    January 2018
    June 2017
    September 2016
    April 2016
    March 2016
    February 2016
    January 2016
    December 2015

    Categories

    All
    Git
    Https
    Interview
    IOS
    JavaScript
    OA
    Personal
    Web

    RSS Feed

Keep Coding, Keep Living.

  • Portfolio
  • Blog
  • Gallery
  • Lab
  • GitHub
  • About