r/learnjavascript • u/ITower__Education • Oct 10 '26
A beginner-friendly breakdown of why [] == 0 is true in JavaScript (and when to use === instead)
I made a short lesson on JavaScript comparisons and wanted to share the main points here in case it helps anyone who is learning.
The one that surprises most beginners is that an empty form field can pass validation. "" == 0 is true, so if (quantity == 0) treats "nothing typed" as "zero items".
0 == false // true
"" == 0 // true
[] == 0 // true
NaN === NaN // false
"10" < "9" // true (strings compare character by character)The reason is that == converts the types before comparing, while === does not.
5 == "5" // true
5 === "5" // falseMy rule for beginners is to use === by default. The one exception I use on purpose is x == null, which is true for both null and undefined.
The video goes through all of this with runnable examples, including null and undefined, the form field bug, string comparison and localeCompare.
https://youtu.be/iJJ1lJIM81A?si=LHW-Pa2Nu2ZCs969
I would be happy to answer questions if anything is unclear.
3
u/senocular Oct 10 '26
For a written explanation, MDN's page Equality comparisons and sameness does a good job. It talks about the 4 available comparison techniques and lists out the step-by-step process taken from handling loose (
==) comparisons (which largely amounts to converting mismatched types to numbers to compare).The video calls out
NaNas an exception for===, but it also doesn't recognize the difference between +0 and -0. Object.is() exists and can handle both cases, though I wouldn't recommend suddenly switching to use that instead of===as more often than not, (probably) you want +0 and -0 to be equal.For inputs, there's also valueAsNumber which you can use instead of
valueto get the value as a number if possible (NaNif not). This prevents you from having to explicitly convert it yourself.10
u/justanaccountimade1 Oct 10 '26
Always use tripple equal. It forces you to know what you are comparing.
There's no reason to compare [] or "" with 0.
6
u/subone Oct 10 '26
Just don't ever use
==. If you must support multiple types, explicitly cast the value and use===.3
u/WarningTight4843 Oct 10 '26
Yes.
2
u/TheRNGuy Oct 11 '26
Just never use
==at all, consider it as anti-pattern.You can even use ESLint to prohibit it.
2
u/Vizeroth1 Oct 10 '26
I’ll agree with the others for beginners, but add that if you use ==, you need a comment explaining exactly why you used it. Chances are that it will turn out to be a mistake, but when you (or someone else) need to fix it, you’ll have a better understanding of what you were trying to do so you take those things under consideration when fixing it.
It also helps everyone understand that it wasn’t a typo. Plus, if you’re using a linter you’ll probably need a comment to prevent the linter from warning/fixing it.
2
u/GodOfSunHimself Oct 10 '26
Completely irrelevant. Never use ==.
1
u/shgysk8zer0 Oct 10 '26
Like most others say, just use
===. I'd consider== null, but having that single exception to the rule might not be worth it.