DEV Community

Discussion on: Is there a reason to use == over ===?

Collapse
 
mellen profile image
Matt Ellen

That's what I want to discuss. What would make == appropriate?

Collapse
 
theaccordance profile image
Joe Mainwaring

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.

Thread Thread
 
mellen profile image
Matt Ellen

That's all evidence why you should use ===.

Would you say that there is no reason to use ==?

Thread Thread
 
theaccordance profile image
Joe Mainwaring

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.