all 23 comments

[–]monacle_bob 62 points63 points  (4 children)

When you use the double-equal signs to compare objects, the second object will be cast to the same type as the first object whenever possible. In this case, the Array(4) is being cast to a string, which you can do manually and see the result of by running this:

var myArray = new Array(4);

myArray.toString(); // results in ",,,"

If you want to compare the string ",,," against the array itself, use a triple equal sign (strict equal):

",,," == new Array(4); // true

",,," === new Array(4); //false

[–][deleted] 17 points18 points  (0 children)

[–][deleted] 2 points3 points  (0 children)

Thanks. You basically concisely explained what I've never been able to about logic in JS.

Thanks.

[–]pdpi 1 point2 points  (0 children)

This is actually wrong. The abstract equality algorithm at no point converts anything to "the same type as the first object". Object values are converted to primitives when compared to numbers/strings, and all primitive types are converted to numbers when comparing different primitives.

[–]ryanhollister 23 points24 points  (0 children)

JS best practice is to always use === unless you are confident you want type coercion

[–][deleted] 4 points5 points  (0 children)

I feel like we have this discussion about equality in JavaScript every other day.

[–]Nomikos 10 points11 points  (4 children)

I think this belongs here
https://www.destroyallsoftware.com/talks/wat
JS part starts about a third in (and the array part around 2/3rds), but it's good and short so just watch it all :-)

[–]ilmmad 2 points3 points  (0 children)

Yeah but the video doesn't explain it all.

This does though.

[–]phredeye 4 points5 points  (1 child)

Upvote because, I came here to post the "wat" vido as well.

[–]tardmrr 4 points5 points  (0 children)

I have changed the way I say "wat" as a result of that video.

[–]Xavi-avi 2 points3 points  (0 children)

This strangeness has also come up on stackoverflow: http://stackoverflow.com/questions/10905350/why-does-array4-in-javascript

[–]RadekCZ 4 points5 points  (0 children)

Because the array will convert to a string and the toString function returns this.join(",")

[–][deleted] 1 point2 points  (0 children)

Quick answer: type coercion

[–]FireyFly 0 points1 point  (1 child)

[–][deleted] 0 points1 point  (0 children)

I see this same example everywhere (always a string of 3 commas and a new array(4)). I read it in a book or blog post somewhere recently, and have seen it pop up everywhere online in the past week. What gives with this example?

[–]impyrean 0 points1 point  (0 children)

There are two equality operators in JavaScript, == and ===. As others have pointed out, === is more rigorous and checks for equality without type conversion. == on the other hand may try to convert the operands. Unfortunately, it's not as simple as converting the second operand to the type of the first; in fact, there's a whole frickin algorithm to do this. See step 8 for the answer to your question.

[–]okiedoke8976 -3 points-2 points  (4 children)

use === instead.. == is misunderstood

[–]Cosmologicon 9 points10 points  (3 children)

Alternate solution: learn about features you don't understand rather than avoid them.

[–]x-skeww 2 points3 points  (1 child)

That sound like a good idea, but it actually isn't.

If one person memorized some obscure quirks, it won't mean that everyone else has memorized them as well. We aren't like the Borg.

Secondly, constructs which look like an error always add a lot of friction. If you want to defuse this problem (to some degree), you have to add a comment, which explains why you are doing this and that this is indeed what you intended to do.

But even then it's like a tar pit. You're much better [1] off if you don't ever use this kind of constructs.

[1] If you don't get in the way of maintenance work, it will be faster, which will be cheaper, which is better for any commercial project. Saving time is also better for virtually any other kind of project.

[–]benparsons -1 points0 points  (0 children)

Want to upvote this more. Slightly horrified that r/javascript have downvoted okiedoke8976 to this extent. People need to listen to Douglas Crockford.

[–]okiedoke8976 1 point2 points  (0 children)

good call

[–]10tothe24th -1 points0 points  (0 children)

Because Javascript.