# Why on earth does null == "0" give false as a result?

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 …

Thanks

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”.

@anon52159105 - thanks, i did not get the last part

if given a non-numerical value it will return “NaN” (not a number error). Because of this null is not equal to “0”.

In console when i type Number(null) , its giving 0 , can you please guide a bit more …

Thanks

`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”.

Here is a table you can always check if you are unsure

Also, give chapter 4 a read you will find some sort of explanation there.

2 Likes

@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…

1 Like

@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 ?

Thanks

Null is not equal to 0 because null is a value, it as far as the language is concerned is nothingness.

`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.

2 Likes

0 is a number, null is nothing.

You can’t put the definition of null as itself, so they put 0 to represent no value.

Prove me wrong, but by logic that’s what I would think.

I see thanks! (20 character)

I edited the title to make it grammaticaly correct.

1 Like

If `"0"` could be coerced to equal `null` then I’d expect it to also coerce to `false`, which would be strange because `if("0") {}` would be ignored.

Actually, PHP will treat `"0"` as a falsy value, and I feel Javascript got things right in this case.

Zero is a numeric value, null is a reference to nothing.