TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

In JavaScript, why does (null + null === 0) return true?

1 pointsby swozniakabout 11 years ago
I could be convinced into thinking == would return true if null was treated as 0 (so 0 + 0 = 0), but with ===, this doesn&#x27;t seem right at all, especially considering:<p>typeof null === &#x27;object&#x27;<p>and<p>typeof 0 === &#x27;number&#x27;<p>Can someone explain?

3 comments

singoldabout 11 years ago
I don&#x27;t really know (I don&#x27;t have that in depth knowledge of javascript) but it is probably like you said, because you are doing a sum, it treats null as 0, so (0 + 0 === 0) is true.<p>You can do the same with false: (false + false === 0)<p>And also (true + true === 2) is true<p>Edit: The same way as if you do &quot;&quot; + 1 you get &quot;1&quot; because it assumes + as string concatenation
joelbirchlerabout 11 years ago
The + operation is evaluated first, and + coerces nulls to zeros. This looks like:<p>(null + null) === 0<p>(0) === 0<p>true
评论 #7705653 未加载
zamfiabout 11 years ago
&gt; null + null<p>0<p>&gt; +null<p>0<p>Looks like JS converts null to 0 when it&#x27;s used in a number context.