search

Home  >  Q&A  >  body text

javascript - Why does !function foo(){} return false and !function foo(){}() return true?

As mentioned, thank you!

女神的闺蜜爱上我女神的闺蜜爱上我2814 days ago810

reply all(2)I'll reply

  • 淡淡烟草味

    淡淡烟草味2017-06-30 10:00:06

    The only ones judged as false in

    js are the following:

    • 0

    • NaN

    • false

    • ''

    • null

    • undefined

    Therefore:

    !function foo () {} == !(function foo () {}) == false
    !function foo(){}() == !(function foo () { return undefined })() == !undefined == true

    reply
    0
  • 高洛峰

    高洛峰2017-06-30 10:00:06

    I think your question can be changed to !(function foo(){}) returns false, !(function foo(){})() returns true?
    Because the code function foo(){}() will return Uncaught when executed SyntaxError: Unexpected token error.

    The former is to negate the non-empty string. The Boolean value of the non-empty string is true. After negation, it is naturally false;
    The latter is to negate the return value of the self-executing function. The return value is undefined and converted into a Boolean value. is false, and its negation is true.

    The above are personal opinions, welcome to discuss. Thanks!

    reply
    0
  • Cancelreply