15

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

Thanks!

Community
  • 1
  • 1
7wp
  • 12,505
  • 20
  • 77
  • 103
  • 1
    After reading everyone's answers, now I understand why JSLint's slogan is "JSLint will hurt your feelings." – 7wp Jul 28 '10 at 22:14
  • See [Why avoid increment ("++") and decrement ("--") operators in JavaScript?](http://stackoverflow.com/questions/971312/why-avoid-increment-and-decrement-operators-in-javascript) – Matthew Flaschen Jul 29 '10 at 22:20
  • @Matthew, why did you vote to close my question? The other one you linked does not address my question about == and != (although it does talk about the ++ and --) – 7wp Jul 30 '10 at 06:52
  • I see your point. Maybe I shouldn't have, but it seemed like you mostly understood the ==/=== issue. – Matthew Flaschen Jul 30 '10 at 20:21
  • @Matthew, yep, I understand the difference, I just wasn't clear on the 'why' part, and that was really supposed to be the point of my question (the 'why'). – 7wp Aug 03 '10 at 16:33

7 Answers7

12

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) {}

VS

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:

Christian C. Salvadó
  • 807,428
  • 183
  • 922
  • 838
  • I think '0' == 0 evaluating to true makes sense to me, and it gives me flexibility allowing me to write less code. – 7wp Jul 28 '10 at 22:09
  • analogy: i know all about static typing, but i still want my compiler to enforce it for me. – Dustin Getz Jul 28 '10 at 22:26
  • @7wp: A problem with mixed-type comparisons is that `someNumber == someString` doesn't indicate whether the programmer cares what happens if different methods of evaluation would yield different results, or would expects that the operands will always be such that all methods of evaluation would yield the same result. IMHO, a good language should only allow such comparisons in cases where there is no ambiguity, and where transitivity is upheld. – supercat Feb 05 '15 at 00:29
10

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
  • 951,095
  • 183
  • 1,149
  • 1,285
  • 2
    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. – 7wp Jul 28 '10 at 21:49
  • 9
    I recommend reading [Javascript: The Good Parts](http://books.google.co.nz/books?id=PXa2bby0oQ0C) for full details on Crockford's point of view. – Greg Hewgill Jul 28 '10 at 21:50
5

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

http://www.youtube.com/watch?v=hQVTIJBZook#t=14m45s

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.

lucas1000001
  • 2,740
  • 1
  • 25
  • 22
  • Thanks for the link, I'll watch it. – 7wp Jul 28 '10 at 22:01
  • That video was helpful, it explains a lot. Now I'm interested to read his book too. – 7wp Jul 29 '10 at 16:34
  • 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 ;) – lucas1000001 Jul 29 '10 at 19:41
3

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.

and

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.

2

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;
Guffa
  • 687,336
  • 108
  • 737
  • 1,005
1

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

Jose Diaz
  • 5,353
  • 1
  • 31
  • 29
0

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
  • 87,823
  • 39
  • 148
  • 191