I am aware that it's prefereable to test equality with ===
because of the nature of truthiness and falseyness in JS, but what are the reasons to use ==
?
Is ==
faster? It doesn't type check, but it does coërce types, which sounds costly.
I suppose if you have to deal with really old browsers that don't have strict equality, but that seems like a super edge case.
Does anyone have reasons for using ==
?
Top comments (15)
usually for nil
returns true if value is
null
orundefined
you can check this logic out in dev console
This is the only good example I can think of for using
==
If you accept user input from a text box, that input is a string. If the textbox is meant to collect a number, it would be easier to use
==
to compare as you get the string coerced (or is the number is coerced?). So cleaner code.This is interesting. My gut reaction is that you should convert to the correct type ASAP, so you don't have to do it multiple times later.
But maybe the comparison before conversion is good so that you don't convert values you aren't going to use?
Still, I would do a comparison with
===
, using a string representation of the number, rather than comparing to a number if that's the case.I still use javascript and not typescript.
I'm quite happy to use == everywhere in my code.
=== would be good if you use typescript and are sure of all your types.
I'm not sure this reasoning is sound.
TypeScript gives you type notation, so you know what types your variables are. Thus
===
should be redundant, because you shouldn't need to type check.With JavaScript types are not as obvious. Comparing with
==
is error prone because it will try to coerce your variables to be the same type, whereas with===
it won't. Sometimes in JS types matter, so using===
is preferable so you don't end up assigning a string to a number or vice versa.In javascript I want to compare 4 == "4" and get true.
In typescript you should get and error before compile if you try to compare 4 == "4" I guess...
When I saw the === the first time, I thought it was for "deep" comparison of objects :-)
ie: {a:1, b:2} === {b:2, a:1} should be true.
So I guess we still have no answer why to use === rather than == in everyday JS code.
I've never had a reason to say "types does matter" - mysql and json is happy saving a string to a number and vice versa. Displaying entry fields or text in html as well.
I guess in a accounting environment maybe? Negatives, decimals, power of 10, etc.. You would want to make 100% sure you have a number, and if user type 10e2.123 you would need to convert to 1000.12 before saving?
I see I use parseInt if I want to add numbers.
ie:
return this.meritList.reduce( (accum,item) => accum + parseInt(item.point),0)
And that's about it.
The question was why to ever use
==
rather than the===
.You're saying you never use
===
in JavaScript? Do you not fall foul of the problem where3+3
is not the same as'3'+3
? Or that'0' == 0
but0 == false
and'0' == true
?I did a quick grep - and I see there is some place where I use ===
if (!response.constructor === Array)
if (this.photoPath === undefined)
But I use == much more frequently
Never realised that 0 == false and '0' == true - that is a nasty one.
But I've never when debugging, had an error related to == vs ===
Lucky or dumb, or dumb luck I guess?
About old browsers. ES3 spec (around 2000) contains === operator. So I do not think that we need to consider that edge case at all.
Double equals (
==
) compares the valueTripple equals (
===
) compares the value and the typeNow, I'd say best practice is to standardize around
===
when it comes to producing quality code in a collaborative environment, but that's opinion. Do what makes sense for your projectThat's what I want to discuss. What would make
==
appropriate?Appropriate is subjective, but it would involve needing to compare to identical values that are different types.
For example, let’s say we have two variables: X = 3 and Y = “3”
X == Y will return as true, but X === Y would return as false, because Y is technically a string type and X is a number type.
Now, I find it pretty trivial to set the two variables to the same type before doing a comparison, but that may not be the case for everyone. I could use ‘parseInt’ for example Which would turn Y from a string type to a number type.
That's all evidence why you should use
===
.Would you say that there is no reason to use
==
?Never say never, there’s probably some context out there where it still makes sense. It would be safe to take the position of defaulting to === and if you encounter an exception, then you make that decision.
Just use tripple always and forget about it