Javascript The Bad Parts: Comparison Operators

When I search online for good JavaScript code examples there is one thing that I immediately look for inside that code. Does it have == or === comparison operators (double or triple equal signs). The reason for doing that is that it gives me an idea of how reliable the code is. I understand that this is not the only reliability indicator, but it is pretty important. However, I have noticed that during the last couple of years many JavaScript developers became aware of the possible pitfalls of == (double equal) comparison. Maybe because of Douglas Crockford, maybe because of JSLint or JSHint. Nobody knows why, but I definitely find less and less examples with == in the code…

So, what is all the fuzz about these comparison operators? Well you might have guessed it – they are one of the bad parts of JavaScript language. Consider this example:

*Example taken from Douglas Crockford’s book: JavaScript the Good Parts, which I highly recommend for new JS developers.

What was going through the heads of JavaScript language creators when this was allowed to happen?! All in all, the general suggestions would be to use the === and !== comparison operators. Unless, you are a seasoned JavaScript developer avoid == and != . Even if you are seasoned developer there must be a very specific reason why you choose to use the less predictable comparison operators, right?

Leave a Reply