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.

JavaScript Equality Table

160 pointsby antoinecover 10 years ago

19 comments

dustingetzover 10 years ago
There is a followup article &quot;Don&#x27;t Make Javascript Equality Look Worse Than It Is&quot;<p>&quot;These posts are fundamentally right about == being poorly designed... but they make things look worse by failing to organize the table.... What a mess! But most of the mess is because of the order of values in the table. By grouping equal values together, you get something more sensible&quot;<p><a href="http://strilanc.com/visualization/2014/03/27/Better-JS-Equality-Table.html" rel="nofollow">http:&#x2F;&#x2F;strilanc.com&#x2F;visualization&#x2F;2014&#x2F;03&#x2F;27&#x2F;Better-JS-Equal...</a>
评论 #8801801 未加载
sheetjsover 10 years ago
The rules are fairly simple: <a href="http://www.ecma-international.org/ecma-262/5.1/#sec-11.9.3" rel="nofollow">http:&#x2F;&#x2F;www.ecma-international.org&#x2F;ecma-262&#x2F;5.1&#x2F;#sec-11.9.3</a>
评论 #8801126 未加载
评论 #8801080 未加载
judahover 10 years ago
I like the end of the article. &quot;Use three equals unless you fully understand the conversions that take place for two-equals.&quot;<p>Or better said, &quot;Always use 3 equals unless you have a good reason to use 2.&quot;
评论 #8800417 未加载
评论 #8801307 未加载
farnsworthover 10 years ago
<p><pre><code> if (2) { console.log(&#x27;yes&#x27;) } &gt; yes &gt; undefined if (2==true) { console.log(&#x27;yes&#x27;) } &gt; undefined </code></pre> I would have thought these would be the same - what&#x27;s the difference between being &quot;truthy&quot; in the first case, and being ==true, as in the second?
评论 #8800445 未加载
评论 #8800434 未加载
评论 #8800446 未加载
评论 #8800440 未加载
jessedhillonover 10 years ago
Most of these scenarios are mitigated by knowing what types of data are coming into your functions. If you&#x27;re defending against a string being compared to a number, I&#x27;d sooner wonder why you don&#x27;t know whether you have a string or a number in your variable in the first place. What could cause you to attempt to use `[1]`(array containing number 1 as an element) as a boolean? Seems like you should know if a variable contains an array and not true&#x2F;false, and if you don&#x27;t then that&#x27;s a better place to focus your attention.<p>Happy to hear counterexamples though.
评论 #8801222 未加载
ambrop7over 10 years ago
It immediately grabbed my attention that they used images for the column labels. Vertical text should be doable with CSS.
评论 #8801070 未加载
thisisblurryover 10 years ago
This reminds me of Miłosz Kośmider&#x27;s &quot;Zeros in JavaScript&quot; comparison table: <a href="http://zero.milosz.ca/" rel="nofollow">http:&#x2F;&#x2F;zero.milosz.ca&#x2F;</a><p>Essentially the same thing, but covers a few more operations.
userbinatorover 10 years ago
All dynamically typed languages with implicit coercion will have similar-looking tables:<p>PHP: <a href="http://www.blueshoes.org/en/developer/php_cheat_sheet" rel="nofollow">http:&#x2F;&#x2F;www.blueshoes.org&#x2F;en&#x2F;developer&#x2F;php_cheat_sheet</a><p>Perl: <a href="http://qntm.org/equality" rel="nofollow">http:&#x2F;&#x2F;qntm.org&#x2F;equality</a><p>Python: <a href="http://i.stack.imgur.com/Ya0Ux.png" rel="nofollow">http:&#x2F;&#x2F;i.stack.imgur.com&#x2F;Ya0Ux.png</a> (I don&#x27;t think all the entries are correct)
评论 #8802236 未加载
评论 #8830530 未加载
stefek99over 10 years ago
Object plus object is not a number :)<p><a href="https://www.destroyallsoftware.com/talks/wat" rel="nofollow">https:&#x2F;&#x2F;www.destroyallsoftware.com&#x2F;talks&#x2F;wat</a>
评论 #8801325 未加载
评论 #8801114 未加载
jakub_gover 10 years ago
Interesting, I didn&#x27;t know about `[1] == true`, `[0] == false` family of equalities.<p>Edit: it seems more general, `42 == [42]` etc. holds.<p>Edit: more fun<p><pre><code> ([0])==false &#x2F;&#x2F; true !([0])==false &#x2F;&#x2F; true</code></pre>
评论 #8800787 未加载
评论 #8801118 未加载
prezjordanover 10 years ago
Well, at least it&#x27;s got some symmetry to it!
评论 #8800086 未加载
评论 #8800087 未加载
rnhmjojover 10 years ago
&quot;[] == []&quot; and also with &quot;===&quot; is false. Why they choose not to use these for array comparisons?
评论 #8802277 未加载
kelvin0over 10 years ago
Going to the link made my Firefox (34.0.5) allocate over 2 GB of RAM... and then everything stalled to molasses...
shurcooLover 10 years ago
Can someone do this table for Go? I&#x27;m just curious to see it.
评论 #8800983 未加载
评论 #8801976 未加载
评论 #8801520 未加载
SimeVidasover 10 years ago
<i></i>tl;dr<i></i> ignore == and !=, use === and !== instead
duxetover 10 years ago
i think this table was already posted many times - but this equality table thing never gets boring :D
Bugeover 10 years ago
It leaves out negative zero.
nijo108over 10 years ago
At least it is commutative.
frou_dhover 10 years ago
dynamic typing good<p>weak typing bad