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.
An important distinction to make is that the
TypeofnullisNull.(ignore
typeofit returns"object"fornullbecause of bad design and backwards compatibility)The ES5 specification
Says that the comparison with
nulland aBooleanshould return false because theTypeofNullandBooleanare not the same, and none of the other steps in 11.9.3 apply so the default action ofreturn falsehappensThe only case where the
Typeofxandyare different and eitherxoryisnullbut the==operation still returnstrueareThat means
undefined == nullreturnstrueOf special note:
There is an ES6:harmony proposal to fix
typeof null