What does “!” operator mean in javascript when it is used with a non-boolean variable?

后端 未结 5 439
小鲜肉
小鲜肉 2020-12-03 10:53

While reading through javascript codes I\'ve been seeing the ! operator used for non boolean variables. Here is an example of code not used in.

         


        
相关标签:
5条回答
  • 2020-12-03 11:25

    Any falsy value will satisfy the if(!insert_variable_here) condition, including:

    • false
    • null
    • undefined
    • The empty string ''
    • The number 0
    • NaN

    If callback return evaluates any of those values, the condition will be satisfied.

    Even though null != false, the following will give you an alert:

    x = null;
    if(!x) {
        alert('"!null" does evaluate to true');
    }
    

    Regardless of whether or not null != false makes sense to you or anyone else, the point is that in JavaScript null is a falsy value, and thus a value that would satisfy the condition in my first bit of code listed above. This, it seems, is the question you have asked--not, rather, if null should or should not == false.

    0 讨论(0)
  • 2020-12-03 11:27

    It means NOT or false.

    It is simply an alternative to:

    if (callback != null)
    {
         //callback is not null
    }
    
    if (callback)
    {
        //callback is not null
    }
    

    OR

    if (this.relativeTo && relativeTo == false)
    {
        //some code
    }
    
    0 讨论(0)
  • 2020-12-03 11:35

    In JavaScript, the unary negation operator (!) will convert its operand into a boolean based on the (somewhat confusing) rules of the language (e.g., ECMA-262 5th Edition). This article on JavaScript syntax shows some examples of how the type conversion happens.

    Basically, it's an easy way to test for non-"truthiness"; seemingly false values (e.g. false, null, 0, NaN, the empty string, etc.) will be converted to false before being logically negated, and vice versa. You can test for "truthiness" explicitly by using the Boolean constructor:

    Boolean(null); // => false
    Boolean(NaN); // => false
    Boolean(""); // => false
    Boolean(0); // => false
    Boolean(1); // = >true
    Boolean("123"); // => true
    Boolean(new Object()); // => true
    Boolean(function(){}); // => true
    
    0 讨论(0)
  • 2020-12-03 11:46
    !object
    

    will return false when the object is undefined, equals to null or 0, true otherwise.

    0 讨论(0)
  • 2020-12-03 11:49

    Every variable and object can be equated to true or false. In the case of objects, undefined is false, anything else is true.

    This actually gives some interesting edge cases http://www.ejball.com/EdAtWork/2005/02/15/JavaScriptWhatIsTrue.aspx

    One that kinda blows my mind null != false evaluates to true

    UPDATE

    Note for all those people bashing me in the comments the ECMAScript specification on page 43 defines the toBoolean conversion of null as false. As we're only using a == and not doing a type check with === I find it very reasonably that I would expect null == false to evaluate to true.

    That specification was published June this year so maybe this is a change, but it appears the specification writers come down on my side of the table in this debate.

    0 讨论(0)
提交回复
热议问题