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 Minesweeper

185 pointsby bennettfeelyover 6 years ago

15 comments

snekover 6 years ago
I see a lot of comments here suggesting that == is okay to use if you know what you're doing or if the types on both sides match or if this or if that. Writing idiomatic code is all about being expressive and being explicit. Even in the most common case, testing for null or undefined, it's absolutely not clear what is actually happening or what the intent is. I write a lot of JS, and I've never had any justifiable reason to use ==.
评论 #18006523 未加载
评论 #18005815 未加载
评论 #18006154 未加载
评论 #18006064 未加载
评论 #18009158 未加载
spionover 6 years ago
The problem with this and the wat talk is that<p>* you don&#x27;t actually see the case-space (value space) of all the comparisons that <i>do</i> work as expected, and<p>* you don&#x27;t a sense of what is the likelihood that these sort of comparisons would happen in real world code<p>Some of them like the empty string are likely to happen from user input, but Typescript mitigates those by forcing you to e.g. use Number(inputField.value) to conver to number and complaining about the assignment otherwise.<p>Others pretty much never ever happen - instead of comparing 1 or -1 to true, you&#x27;re more likely to use if (val) which casts to boolean, and the truthy table is different from the equality comparison table (it makes a bit more sense)<p>Most of the real world comparisons are to non-empty strings or numbers, and those are only equal to arrays in some cases - but its rare for an actual array to be produced by anything. Things you know are arrays already you don&#x27;t compare using &quot;==&quot; to begin with.<p>So yeah, in practice the confusing rules of JS equality comparison don&#x27;t really matter all that much.
评论 #18008169 未加载
teddyhover 6 years ago
“Let’s talk about Javascript!”<p><a href="https:&#x2F;&#x2F;www.destroyallsoftware.com&#x2F;talks&#x2F;wat" rel="nofollow">https:&#x2F;&#x2F;www.destroyallsoftware.com&#x2F;talks&#x2F;wat</a>
评论 #18007818 未加载
评论 #18006378 未加载
评论 #18006125 未加载
linkmotifover 6 years ago
Fun game. Really captures the spirit of the subject. Great idea!!<p>I’m not one to pride myself on ignorance, but the JS equality operator is ridiculous and therefore, IMO, not worthy of the mental energy it demands.<p>&gt; How well do you know the rules for the == operator in JavaScript?<p>Well enough to use `===`. I’ve noticed in my code every time I’m tempted to use `==`, I always end up finding a better way. `==` is basically code smell that only really smart people should use.
评论 #18006487 未加载
tttyover 6 years ago
It&#x27;s so funny. Anybody still using == with js?
评论 #18004382 未加载
评论 #18004303 未加载
评论 #18004949 未加载
评论 #18004464 未加载
评论 #18004389 未加载
评论 #18005510 未加载
评论 #18004911 未加载
评论 #18005572 未加载
评论 #18005685 未加载
评论 #18004821 未加载
评论 #18005010 未加载
评论 #18012192 未加载
评论 #18005449 未加载
评论 #18004342 未加载
评论 #18005866 未加载
评论 #18004837 未加载
bazaniover 6 years ago
Well, looks like my knowledge is negative. I clicked the box random and when i asked for my result i was 245% wrong. O_o&#x27;
评论 #18005328 未加载
评论 #18005519 未加载
评论 #18005920 未加载
yoavaover 6 years ago
Super cool application, and can teach a lot about why you should use === instead of == in JavaScript.<p>The only legit case to use == is if you are<p>1. Insane<p>2. The kind of person who changes Java 1 object to equal 2
IvanK_netover 6 years ago
I don&#x27;t think it is useful to know the behaviour of JS in such cases. In practice, you rarely compare strings with numbers, or objects with booleans.<p>I wrote tens of thousands of lines of JS (e.g. this library <a href="https:&#x2F;&#x2F;github.com&#x2F;photopea&#x2F;UPNG.js" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;photopea&#x2F;UPNG.js</a> ), I never used &quot;===&quot; in my life :D
评论 #18004562 未加载
sadjfoadsfover 6 years ago
There&#x27;s a lot of ===ers in here. I can&#x27;t say I&#x27;ve ever experienced a situation where not using === has caused an issue. However, I can think of many cases where === would have caused an issue. In other words, I&#x27;ve found == better handles unintended logical errors. Nonetheless, the equality chart is difficult to memorize. Luckily, I typically only deal with a subset of it.
评论 #18013857 未加载
keymoneover 6 years ago
it seems equality operator in javascript is good for nothing but being a source of confusion and a target of jokes.<p>does it not make sense to just remove it from the language completely? who is deciding that?
评论 #18004877 未加载
评论 #18006719 未加载
评论 #18004545 未加载
评论 #18005505 未加载
评论 #18004908 未加载
brooksideover 6 years ago
<p><pre><code> NaN == NaN </code></pre> is falsy, no? (Shows as truthy in the game, unless I am misunderstanding.)
评论 #18007252 未加载
评论 #18006702 未加载
malmsteenover 6 years ago
True == 1 but False &quot;not ==&quot; 0<p>Wut?
评论 #18008399 未加载
danschumannover 6 years ago
`true == [1]` is news to me
hamandchrisover 6 years ago
There needs to be a PHP version of this.
bacroover 6 years ago
668% wrong!! :D