最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

operators - Why is it a bad idea to allow these in JavaScript == , != , ++ , -- - Stack Overflow

programmeradmin10浏览0评论

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!

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!

Share Improve this question edited Jun 20, 2020 at 9:12 CommunityBot 11 silver badge asked Jul 28, 2010 at 21:45 7wp7wp 12.7k20 gold badges78 silver badges107 bronze badges 5
  • 1 After reading everyone's answers, now I understand why JSLint's slogan is "JSLint will hurt your feelings." – 7wp Commented Jul 28, 2010 at 22:14
  • See Why avoid increment ("++") and decrement ("--") operators in JavaScript? – Matthew Flaschen Commented Jul 29, 2010 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 Commented Jul 30, 2010 at 6:52
  • I see your point. Maybe I shouldn't have, but it seemed like you mostly understood the ==/=== issue. – Matthew Flaschen Commented Jul 30, 2010 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 Commented Aug 3, 2010 at 16:33
Add a comment  | 

7 Answers 7

Reset to default 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:

  • ECMAScript. Equality operators (Great tips to remember how == works)
  • typeof, == and ===

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.

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.

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.

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

发布评论

评论列表(0)

  1. 暂无评论