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++ ?


+10  A: 

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.

Greg Hewgill
Right.. I guess what I want to know is why he would consider those 'good', even if I disagree with it. Just want to know his point of view.
Roberto Sebestyen
I recommend reading [Javascript: The Good Parts]( for full details on Crockford's point of view.
Greg Hewgill
+1  A: 

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.


+3  A: 

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.

Mark Trapp
+12  A: 

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:

I think '0' == 0 evaluating to true makes sense to me, and it gives me flexibility allowing me to write less code.
Roberto Sebestyen
analogy: i know all about static typing, but i still want my compiler to enforce it for me.
Dustin Getz

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).

David Murdoch
`--x+++x-x++`. Contrived, but my only argument against `++` and `--`. :)
Brian S
haha. thats not even valid. :-)
David Murdoch
But this is: `--x+ ++x-x++` (just one space added), and it's almost as bad
Without running the code, what will be alerted if the following was run? `var x = 3; alert(--x+ ++x-x++); alert(x);`
David Murdoch
+4  A: 

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.

Thanks for the link, I'll watch it.
Roberto Sebestyen
That video was helpful, it explains a lot. Now I'm interested to read his book too.
Roberto Sebestyen
Glad to hear it! :)His book is also very good, probably the best JavaScript text out there! As you can imagine, it expands greatly on the content in the video.Don't be fooled by it's mere ~150 pages. Probably the most densely packed technical book I've read. Page for page is a bargin.P.S. - I'm not getting comission for this ;)
+2  A: 

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;