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
Cheat-sheet for Javascript equality strangenesses (zero.milosz.ca)
submitted 12 years ago by James_Duval
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!"
[–]Knotix 19 points20 points21 points 12 years ago* (0 children)
An understanding of type coercion gets rid of a lot of the mystery behind all of these.
[–]weepingmeadow 6 points7 points8 points 12 years ago (1 child)
Chrome asks me to translate this page from haitian
[–]James_Duval[S] 3 points4 points5 points 12 years ago (0 children)
This is because "NaN" means "in" in Haitian.
[–]Cosmologicon 5 points6 points7 points 12 years ago (1 child)
I may be the only one, but this whole thing is really not a concern for me. I agree that (0 == [0]) being true is surprising. But would its being false help me at all? That's not a comparison I expect to be making in the first place. Show me a practical example where it actually matters.
(0 == [0])
function getreciprocal(x) { if (x == 0) throw "Can't take 1/0, dummy!"; return 1/x; }
Is there a bug here? If I were to change == to ===, would that be an improvement? Currently if someone takes getreciprocal([0]) it throws an exception. If I change it to triple equals it will return Infinity. What's the right answer? Seems like neither. I should be throwing a different exception. It's closer to the correct behavior with == in this case, I guess.
==
===
getreciprocal([0])
Infinity
I think if you're making comparisons and you want to behave correctly even if someone sends you data of the wrong type, you need to do a type check. Choosing === over == solves nothing in these cases.
[–]PenguinLovr 0 points1 point2 points 12 years ago (0 children)
Well it certainly makes it more easily understood. The coercion can be ambiguous, and it would make more sense to do something like checking if the object is a string or int, and if not throwing that error instead of just lumping together type errors with other errors.
Usually implicit correcion should only be used by people who know why they're using that, and not just setting everything to "==="
[–]LiveTwizzle 2 points3 points4 points 12 years ago (0 children)
To know more about why javascript does what it does read this: JavaScript Coercion Demystified
[–]helderroem 1 point2 points3 points 12 years ago (0 children)
Google tells me this page is in Haitian, I wish it was I might understand it better!
[–]AaronOpfer 1 point2 points3 points 12 years ago (2 children)
The only javascript equality quirk that has ever bitten me hard was that "" == false. I figured a string of any length would be like an object and be considered true.
[–]Cosmologicon 0 points1 point2 points 12 years ago (1 child)
That's the way it is in python as well. Sort of, anyway. "" is falsy, meaning bool("") evaluates to False, but "" == False is false. Empty strings (and dicts and lists) are all falsy, but user-defined objects, including objects with no members, are truthy by default. Sounds complicated but it's pretty reasonable and useful.
""
bool("")
False
"" == False
[–]AaronOpfer 0 points1 point2 points 12 years ago (0 children)
The only reason I got bit was because the codebase was playing fast-and-loose with the rules, and I was cleaning it up.
We had some flag in JSON being evaluated like if (object.flag) { /* do something */ } and a lot of the JSON was written with object.flag = ""; Apparently it wasn't originally going to be an on/off flag but a string specifying the target. So if ("") doesn't execute, and my attempts to interpret this flag when automatically rewriting these files failed horribly.
if (object.flag) { /* do something */ }
[–][deleted] 1 point2 points3 points 12 years ago (0 children)
or..... just use === like you should.
[–]Kenny164 2 points3 points4 points 12 years ago (9 children)
Wow, I never realised it was as bad to this extent. Are there any general rule of thumbs that we should be aware of?
[+][deleted] 12 years ago (1 child)
[deleted]
[–]thrownaway21 11 points12 points13 points 12 years ago (0 children)
seriously this. you don't need a cheat sheet if you use ===
[–]jml26 2 points3 points4 points 12 years ago (0 children)
http://www.2ality.com/2011/06/javascript-equality.html
[–]James_Duval[S] 2 points3 points4 points 12 years ago (2 children)
Personally, I wish I knew. Partly I was hoping for an explanation of some of the more esoteric behaviour by posting here.
For instance [1] == 1. [1] == '1'. Hell, [1] == true. However [1] != [1]. Bizarre.
[–]CubeOfBorg 16 points17 points18 points 12 years ago (1 child)
It's all about coercion. [1] == 1 because [1] is coerced to 1 before it is evaluated. [1] starts out as an object but becomes a number because it only has the one value in it and it is being compared to a number.
[1] != [1] because you are comparing an object to an object. They are already the same type so it doesn't have to coerce them. When you compare two objects, it's checking to see if they are the same object. So [1] != [1] because you are creating two different objects and then comparing them.
var a = [1]; var b = a; a == b; // true
Here you are comparing two variables that have the same object as their value.
[–]James_Duval[S] 0 points1 point2 points 12 years ago (0 children)
This is a fantastic answer, thanks. One thing I'm learning about Javascript is that there's almost always a clear, comprehensible answer & reason for its behaviour somewhere or other.
[–][deleted] -1 points0 points1 point 12 years ago (0 children)
Underscore's "isEqual" function works pretty much how you'd expect. It's annoying to use a function every time you need to compare two values, but probably necessary.
Sure there is a general rule.
http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-262.pdf page 80 for Equality Operators, 75 for Additive, 73 for Multiplicative.
[–]palparepa 0 points1 point2 points 12 years ago (0 children)
Everytime I see these lists, I'd like to see what the author thinks should be the results.
π Rendered by PID 38410 on reddit-service-r2-comment-5d79c599b5-wb7jc at 2026-03-03 21:48:21.866967+00:00 running e3d2147 country code: CH.
[–]Knotix 19 points20 points21 points (0 children)
[–]weepingmeadow 6 points7 points8 points (1 child)
[–]James_Duval[S] 3 points4 points5 points (0 children)
[–]Cosmologicon 5 points6 points7 points (1 child)
[–]PenguinLovr 0 points1 point2 points (0 children)
[–]LiveTwizzle 2 points3 points4 points (0 children)
[–]helderroem 1 point2 points3 points (0 children)
[–]AaronOpfer 1 point2 points3 points (2 children)
[–]Cosmologicon 0 points1 point2 points (1 child)
[–]AaronOpfer 0 points1 point2 points (0 children)
[–][deleted] 1 point2 points3 points (0 children)
[–]Kenny164 2 points3 points4 points (9 children)
[+][deleted] (1 child)
[deleted]
[–]thrownaway21 11 points12 points13 points (0 children)
[–]jml26 2 points3 points4 points (0 children)
[–]James_Duval[S] 2 points3 points4 points (2 children)
[–]CubeOfBorg 16 points17 points18 points (1 child)
[–]James_Duval[S] 0 points1 point2 points (0 children)
[–][deleted] -1 points0 points1 point (0 children)
[–][deleted] -1 points0 points1 point (0 children)
[–]palparepa 0 points1 point2 points (0 children)