DEV Community

[Comment from a deleted post]
Collapse
 
csirolli profile image
Christian Sirolli

With the double equals, the values are compared after it attempts to covert them to a common type.

With 0 == "0", converting both values to numbers will cause both values to be 0. Similarly, converting both values to strings will cause both values to be "0".

With 0 == [], converting both values to numbers will cause both values to be 0. Similarly, converting both values to arrays will cause both values to [].

With "0" == [], converting both values to strings will cause the left value to be "0" and the right value to be "", so it returns as false. Similarly, converting both values to arrays will cause the left value to be ["0"] and the right value to be [], so it returns as false.

It may be annoying if you don't understand it, but it is logical and be very useful when appreciated.

Collapse
 
_hs_ profile image
HS

Not sure I can agree on "understanding". Been using all sorts of languages for more than 10 years now and JS is quite inconsistent. I quite well understand it but dislike the approach. For me it's about taste. Ex. If I always get to convert the values for comparison why use == ever and why use dynamic typing? Using if where you don't care about type is fun but unpredictable as in what to put there? Always use .toString()? Always to number? To array? If I it depends on the use case why not always be strict about type since I know what shod be there?

It's a double edge sword. You win some you loose some