Why does “undefined equals false” return false?

后端 未结 7 1066
借酒劲吻你
借酒劲吻你 2020-11-30 06:28

When I compare undefined and null against Boolean false, the statement returns false:

undefined == false;
null == false;

It return false. W

相关标签:
7条回答
  • 2020-11-30 06:43

    This is so because it is so. :)

    Read the ECMA standards here: https://www.ecma-international.org/ecma-262/5.1/#sec-11.9.3

    0 讨论(0)
  • 2020-11-30 06:46

    From the incomparable MDN, sponsored by the company of JavaScript's creator.

    JavaScript provides three different value-comparison operations:

    • strict equality (or "triple equals" or "identity") using ===,
    • loose equality ("double equals") using ==,
    • and Object.is (new in ECMAScript > 6).

    The choice of which operation to use depends on what sort of comparison you are looking to perform.

    Briefly, double equals will perform a type conversion when comparing two things; triple equals will do the same comparison without type conversion (by simply always returning false if the types differ); and Object.is will behave the same way as triple equals, but with special handling for NaN and -0 and +0 so that the last two are not said to be the same, while Object.is(NaN, NaN) will be true. (Comparing NaN with NaN ordinarily—i.e., using either double equals or triple equals—evaluates to false, because IEEE 754 says so.) Do note that the distinction between these all have to do with their handling of primitives; none of them compares whether the parameters are conceptually similar in structure. For any non-primitive objects x and y which have the same structure but are distinct objects themselves, all of the above forms will evaluate to false.

    For a visual overview of the whole picture of equality in JavaScript: https://dorey.github.io/JavaScript-Equality-Table/

    The truth is, this seemingly "bad" aspect of JavaScript is a source of power when you understand how it works.

    0 讨论(0)
  • 2020-11-30 06:48

    You question is half, as we compare undefined/ null to any other types. we will have false return. There is no coercion happening, even we are using == operator.

    0 讨论(0)
  • 2020-11-30 06:51

    According to the specification which was mentioned above:

    If Type(y) is Boolean, return the result of the comparison x == ToNumber(y).

    Number(undefined) = NaN;
    
    false == NaN // false
    

    Moreover if we change order false == undefined

    If Type(x) is Boolean, return the result of the comparison ToNumber(x) == y.

    Number(false) = 0;
    0 == undefined
    

    There is no rule for this case, so work the default behavior:

    Return false.

    0 讨论(0)
  • 2020-11-30 07:02

    Undefined is not the same thing as false, false is a boolean object (which has a value of 0 therefore it is indeed defined).

    An example:

    var my_var;
    var defined = (my_var === undefined)
    alert(defined);  //prints true.  It is true that my_var is undefined
    
    my_var = 22;
    defined = (my_var === undefined)
    alert(defined);  //prints false.  my_var is now defined
    
    defined = (false === undefined)
    alert(defined);  //prints false, false is defined
    
    defined = (true === undefined)
    alert(defined);  //prints false, true is defined
    
    0 讨论(0)
  • 2020-11-30 07:03

    So undefined really means undefined. Not False, not True, not 0, not empty string. So when you compare undefined to anything, the result is always false, it is not equal to that.

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