Null is type object, so it's truthy? What'

2019-06-26 13:34发布

问题:

I'm reading in my book, "Elegant JavaScript", that null == true evaluates as false. Using an interpreter, I have confirmed this to be TRUE. However, later in the chapter--in fact, on the same page--, it says that when null is given as the condition of an if, while, or for statement, it will be converted to a Boolean and return false.

Can anybody with a deeper insight tell me why this is? I know where to find browser source code, but I'm not sure how to target the programming that is responsible for this peculiar and unintuive behavior. Because I know very little C++, I would also appreciate any tips on finding info like this, independently.

Thank you.

回答1:

An important distinction to make is that the Type of null is Null.

(ignore typeof it returns "object" for null because of bad design and backwards compatibility)

11.9.3 The Abstract Equality Comparison Algorithm # Ⓣ The comparison x == y, where x and y are values, produces true or false. Such a comparison is performed as follows:

[... stripped]

  1. Return false.

The ES5 specification

Says that the comparison with null and a Boolean should return false because the Type of Null and Boolean are not the same, and none of the other steps in 11.9.3 apply so the default action of return false happens

The only case where the Type of x and y are different and either x or y is null but the == operation still returns true are

If x is null and y is undefined, return true.

If x is undefined and y is null, return true.

That means undefined == null returns true

Of special note:

There is an ES6:harmony proposal to fix typeof null



回答2:

Actually I think he refers to the fact that typeof null == 'object', which is unfortunately the case. But that's a peculiarity of the typeof operator, not of null itself. Null is falsy value, but typeof returns "object" for it, according to the spec: http://bclary.com/2004/11/07/#a-11.4.3



回答3:

When you compare null to true, it's evaluated as false, so it's not equal to true. Similarly, when used in any other context where it must be treated as a boolean — like an if or while expression — it's false.

It's not really correct to say that "null is type object", because it is not. It is null. It doesn't really have any type, (Thanks @Roee Gavirel) It's got it's own type (the null type) because it isn't anything. In other words, if a variable has the value null, that means that it is referring to nothing; no object at all.

edit — crap hold on a sec because my brain's still asleep.

OK here's what's in the spec. If one of the operands of == is boolean, then the boolean is converted to a number (yes really) and the conversion proceeds that way. That's why null is == to neither true nor false. The "strangeness", therefore, is not so much about null as it is about the complicated rules for evaluating == comparisons.

Section 11.9.3 of the ECMA-262 standard spells it all out in a more-or-less understandable way. Suffice to say that the semantics of == are not at all simple.



回答4:

I don't see what the problem...
if (null == true) = false then (null == false) = true.

if "why" is the question, then the answer is ease of use. (probably taken from C) but if you wish to know if a reference is valid or not you can just do:

if (RefValue) {
    //bla bla bla
}

Rather then:

if (RefValue == null) {
    //bla bla bla
}