DEV Community

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

Collapse
 
webjose profile image
José Pablo Ramírez Vargas

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.

Collapse
 
mellen profile image
Matt Ellen-Tsivintzeli

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.