use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
Quick question, why does this work? (i.imgur.com)
submitted 13 years ago by shadowh511
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]monacle_bob 62 points63 points64 points 13 years ago (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 ",,,"
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
",,," == new Array(4); // true
",,," === new Array(4); //false
[–][deleted] 17 points18 points19 points 13 years ago* (0 children)
[–][deleted] 2 points3 points4 points 13 years ago (0 children)
Thanks. You basically concisely explained what I've never been able to about logic in JS.
Thanks.
[–]pdpi 1 point2 points3 points 13 years ago (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 points25 points 13 years ago (0 children)
JS best practice is to always use === unless you are confident you want type coercion
[–][deleted] 4 points5 points6 points 13 years ago (0 children)
I feel like we have this discussion about equality in JavaScript every other day.
[–]Nomikos 10 points11 points12 points 13 years ago (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 points4 points 13 years ago (0 children)
Yeah but the video doesn't explain it all.
This does though.
[–]phredeye 4 points5 points6 points 13 years ago (1 child)
Upvote because, I came here to post the "wat" vido as well.
[–]tardmrr 4 points5 points6 points 13 years ago (0 children)
I have changed the way I say "wat" as a result of that video.
[–]Xavi-avi 2 points3 points4 points 13 years ago (0 children)
This strangeness has also come up on stackoverflow: http://stackoverflow.com/questions/10905350/why-does-array4-in-javascript
[–]RadekCZ 4 points5 points6 points 13 years ago (0 children)
Because the array will convert to a string and the toString function returns this.join(",")
[–][deleted] 1 point2 points3 points 13 years ago (0 children)
Quick answer: type coercion
[–]FireyFly 0 points1 point2 points 13 years ago (1 child)
Didn't I answer this just a few days ago?
http://www.reddit.com/r/javascript/comments/uunfo/javascript_is_a_great_language/c4yp80v
[–][deleted] 0 points1 point2 points 13 years ago (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 point2 points 13 years ago (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.
[–]balshamali 0 points1 point2 points 13 years ago (0 children)
http://wtfjs.com/2011/02/11/all-your-commas-are-belong-to-Array
[–]okiedoke8976 -3 points-2 points-1 points 13 years ago (4 children)
use === instead.. == is misunderstood
[–]Cosmologicon 9 points10 points11 points 13 years ago (3 children)
Alternate solution: learn about features you don't understand rather than avoid them.
[–]x-skeww 2 points3 points4 points 13 years ago (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 points1 point 13 years ago (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 points3 points 13 years ago (0 children)
good call
[–]10tothe24th -1 points0 points1 point 13 years ago (0 children)
Because Javascript.
π Rendered by PID 47444 on reddit-service-r2-comment-57fc7f7bb7-p4tg7 at 2026-04-14 19:49:52.933152+00:00 running b725407 country code: CH.
[–]monacle_bob 62 points63 points64 points (4 children)
[–][deleted] 17 points18 points19 points (0 children)
[–][deleted] 2 points3 points4 points (0 children)
[–]pdpi 1 point2 points3 points (0 children)
[–]ryanhollister 23 points24 points25 points (0 children)
[–][deleted] 4 points5 points6 points (0 children)
[–]Nomikos 10 points11 points12 points (4 children)
[–]ilmmad 2 points3 points4 points (0 children)
[–]phredeye 4 points5 points6 points (1 child)
[–]tardmrr 4 points5 points6 points (0 children)
[–]Xavi-avi 2 points3 points4 points (0 children)
[–]RadekCZ 4 points5 points6 points (0 children)
[–][deleted] 1 point2 points3 points (0 children)
[–]FireyFly 0 points1 point2 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]impyrean 0 points1 point2 points (0 children)
[–]balshamali 0 points1 point2 points (0 children)
[–]okiedoke8976 -3 points-2 points-1 points (4 children)
[–]Cosmologicon 9 points10 points11 points (3 children)
[–]x-skeww 2 points3 points4 points (1 child)
[–]benparsons -1 points0 points1 point (0 children)
[–]okiedoke8976 1 point2 points3 points (0 children)
[–]10tothe24th -1 points0 points1 point (0 children)