4

Each and everyday, I come across questions where the OP asks why this is true while the other one is not.

Most of the times, they don't get closed as duplicates and instead receive upvotes for their puzzling nature. Those who answer also receive a lot of upvotes.

Can someone please create a canonical resource using this spec.

So, these questions can be closed down as duplicate of the canonical answer either by community or thor users.

Some questions:

And many more…

If someone thinks I'm jealous of those receiving upvotes, to be clear, I AM!

6
  • 9
    I'm not quite sure what kind of questions you're talking about. Can you give a few examples?
    – Mysticial
    Sep 1, 2014 at 15:50
  • 1
    Can't this be used as a duplicate/as the canonical? stackoverflow.com/questions/359494/…
    – rene
    Sep 1, 2014 at 16:10
  • @rene I suppose so... so this post is redundant and I can delete it I suppose...
    – Amit Joki
    Sep 1, 2014 at 16:12
  • You only need everybody active in the javascript tag to use that question as a dup and everybody needs to close vote instead of answer...
    – rene
    Sep 1, 2014 at 16:17
  • @rene that is what I was aiming to achieve through this post...
    – Amit Joki
    Sep 1, 2014 at 16:19
  • Personally I've taken the habit to downvote questions that are answered by a few seconds of googling followed by a few minutes of reading of the resulting MDN page. Sep 24, 2014 at 13:10

1 Answer 1

4

Having posted answers to a few of these myself1, I find it difficult to write a canonical answer. We need to consider two cases:

  • "What's wrong with JavaScript where …==…?". These might be closed as duplicates of a canonical question where the answer is Don't compare values of different types if you don't know what it means and Use === if you are not sure.
  • "How/Why is it that in JavaScript …==…?". These are not so easy to answer, usually by hunting down the specific behaviour in the spec.

    Finding an exact duplicate (with the same types) is very difficult. For example, Why does !!1==“1” equal true and !!2==“2” equal false? was erroneously closed as a dupe of truthiness in boolean to numbers comparison - twice! - even if the answers are quite different.

    Creating a canonical answer for these types of questions is rather complicated due to the recursive nature of the abstract equality algorithm, and the number of possible types2 involved - 5 primitive types and various objects that behave differently in ToPrimitive. Also it would mean to explain ToNumber and ToString in detail3, and after all the canonical answer won't be much more than a collection of quotes from the spec.

It might be a good idea though to maintain a collection of possible duplicates in an FAQ or this meta answer, so that they can be selected easily (and quickly, before answers are written) when a new question arises.

1: Sorry for making you jealous. Which doesn't mean I had the highest-voted answers, so I'll join your club :-)
2: Don't forget the extra comment on wrapper types and why they're different from primitives
3: Not counting the extra answer that explains the difference between ToBoolean (!, if, ?:) and comparing for equality with true/false.

5
  • 3
    The vast majority of programmers are not going to need to know all this in such excruciating detail. Massive tl;dr: Use === Sep 1, 2014 at 23:11
  • Yes, that's case #1. But the minority that wants to know these details is the problem… Or do you think we should just point them to the spec?
    – Bergi
    Sep 2, 2014 at 11:18
  • 1
    This canonical already points to the relevant part of the spec. Sep 2, 2014 at 14:58
  • @RobertHarvey: If you mean nalply's answer, that only details the spec of the === operator and distinguishes primitive string from String instances. If you mean that there are links to the spec, those are rarely enough for a good specific answer…
    – Bergi
    Sep 2, 2014 at 16:10
  • +1 I'm always irritated to see those questions, because they're always answered by a careful reading of an easily found page of the MDN, BUT they're really not all the same, the duplicate isn't easily found and it's hard to make a general canonical QA. Sep 24, 2014 at 13:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .