So, i am now totally confused , javascript says one thing and then does totally opposite , i went to articles and videos to read about == and === , they all said == makes values convert to number …
So how on earth when Number(null) = 0 and Number(false) is 0 , do i get null == “0” as false , please guide me here …
The type of null is weirdly considered an object, whileas the type of “0” is a string. == compares the values, === also compares the types. null is not equal to zero, however the Number() function when given no value (null is considered no value) will return “0”, if given a non-numerical value it will return “NaN” (not a number error). Because of this null is not equal to “0”.
null is the same as providing no value to the function, e.g. it’s ignored, because of this the Number() function returns a default value of zero, providing a non-number value though will return “NaN”.
@zhouxiang19910319 “42” is indeed truthy, but “42” == true is not performing a boolean test/coercion at all, no matter what your brain says. “42” is not being coerced to a boolean (true), but instead true is being coerced to a 1, and then “42” is being coerced to 42.`
Ok so this is what i read, it clearly says what i mentioned that == performs check by converting to number …
So is it the case that in case of null, it does not take it as Number(null) , then what does it take the value to be , i could not find any explanation for this edge case , can you please guide a bit more , like what is the value of null coerced to in this case … thanks for your time…
@anon52159105 - i am really sorry i am still a bit confused even after reading after stuff, what does null get coerced to in this case of using == , like null == “0” , does it is attempted to be ToString, ToNumber ?
null does not get coerced into a number. Per the JavaScript specification, null is only loosely equal to null and undefined. It doesn’t get ToNumber, ToString, or any other internal function called on it. Good observation and great question, though.