Tutorial :Why is it a bad idea to allow these in JavaScript == , != , ++ , --


I was checking out JSLint, and some of the rules piqued my interest. Particularly this:

Disallow == and !=

Disallow ++ and --

Why is it a bad idea to disallow these? I understand the first part, basically it wants me to do === instead of ==. I don't understand why though. I understand the difference between the two, I just want to know why is it bad practice. Some times I really want to do == for example so that it would evaluate true for undefined == null

The second one, well I don't understand at all. Does it want me to do myInt += 1 instead of myInt++ ?



Douglas crockford (the guy who wrote JSLint) explains himself in this video :


but basically (as everyone else has mentioned) it's because of the type coercian.

Worth watching the who video to be honest - very interesting and useful.


I don't agree too much with those rules, instead of discouraging the use of ==, I would recommend to learn about type coercion.

The primary reason about why Crockford wants to avoid == is that the comparison rules depending on the types of the operands can make this operator non-transitive, for example, if:

A == B AND  B == C  

Doesn't guarantees that:

A == C  

A real example:

'0' == 0; // true   0 == '';   // true  '0' == ''; // false  

The strict === operator is not really necessary when you compare values of the same type, for example:

 if (typeof foo == "function") { }  

We compare the result of the typeof operator, which is always a string, with a string literal...

Another example, when you compare something against null, == also compares against undefined, for example:

if (something == null) {}  


if (something === null || typeof something === "undefined") {}  

The above two conditions are at the end equivalent, but the first one much more readable, of course if you know about type coercion and how == behaves.

Learning how the == operator works, will help you to wisely decide which to use.

Recommended articles:


Doug Crockford has his own ideas about what is "good" and "bad" in Javascript. Accordingly, JSLint implements these checks, but makes them optional if you don't completely agree with him.

Disallowing == helps prevent you from making mistakes when you really meant ===. Of course this assumes that you never really want to use ==.

Disallowing ++ and -- is a style thing, some people believe they are harder to read than += 1 and -= 1.


From the instructions:

The == and != operators do type coercion before comparing. This is bad because it causes ' \t\r\n' == 0 to be true. This can mask type errors.


The ++ (increment) and -- (decrement) operators have been known to contribute to bad code by encouraging excessive trickiness. They are second only to faulty architecture in enabling to viruses and other security menaces. There is a plusplus option that prohibits the use of these operators.


The == and != operators do implicit converson of the operators if needed, while the === and !== operators don't. The expression 4 == '4' for example will be true, while the expression 4 === '4' will be false.

Preferrably you should know the data types you are dealing with, so that you can do the proper comparisons in the code.

The ++ and -- operators doesn't cause any problems if they are used alone in a statement, but they are often used to make a statement that does more than one thing in a not so obvious way, like:

arr[++idx] = 42;  

which would be clearer as:

idx += 1;  arr[idx] = 42;  


The behavior of the standard equality operators (== and !=) depends on the JavaScript version. So that's one reason for not using them.

Another reason is that the behavior of the = tends to be very vague.

See https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Operators/Comparison_Operators


I understand ==. (the undefined == null thing is an exception)

("0" == false) === true  ("0" === false) === false  

I've never understood the ++ and -- thing though. I don't like doing i+=1 all over my code (it's slower than ++i).

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »