ChatGPT解决这个技术问题 Extra ChatGPT

JSLint Expected '===' and instead saw '=='

Recently I was running some of my code through JSLint when I came up with this error. The thing I think is funny about this error though is that it automatically assumes that all == should be ===.

Does that really make any sense? I could see a lot of instances that you would not want to compare type, and I am worried that this could actually cause problems.

The word "Expected" would imply that this should be done EVERY time.....That is what does not make sense to me.

I ran into this too with JSLint. I made an update from == to === and it actually broke the previously working code.
If your code has more than 100 lines, it will not pass jslint, really, it's impossible.
"Broke" is too strong a word. It changed your code's meaning. If you were doing a myVar == null check, yes, big changes. ;^) Crockford's argument is that it made the code's meaning more precise, and that's hard to argue.

j
jsalonen

IMO, blindly using ===, without trying to understand how type conversion works doesn't make much sense.

The primary fear about the Equals operator == is that the comparison rules depending on the types compared can make the operator non-transitive, for example, if:

A == B AND
B == C

Doesn't really guarantees that:

A == C

For example:

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

The Strict Equals operator === is not really necessary when you compare values of the same type, the most common example:

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

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

Or when you know the type coercion rules, for example, check if something is null or undefinedsomething:

if (foo == null) {
  // foo is null or undefined
}

// Vs. the following non-sense version:

if (foo === null || typeof foo === "undefined") {
  // foo is null or undefined
}

I hate this rule of JSLint. I think the real problem is that people shouldn't use operators in ways that they don't understand (ironically these are often same kind of people who would blindly replace '===' with '=='). Sure, there are a few usual cases that arise when comparing the number 0 with various strings, but if you're comparing unrelated data like 0 == 'this is a string' - Your code probably has bigger problems than double equal! If you know for sure what types you're dealing with and you know how exactly how they interact with ==, then I think you should use it.
@Jon The point of the === operator is code clarity. There is no reasonable situation to use == as it will never be as clear and understandable as the identity operator. It's not about whether you understand the operators or not, it's about using the one which makes your code more easily readable at almost no expense. The only developers that are arguing against the identity operator are solo developers and people who don't work in teams. By definition, people who's code is not reviewed by enough eyes.
I find the == null comparison to be almost essential. The issue becomes even less important if your code is well tested.
There are in fact times when using == is essential, in order to perform the required test. if foo.toString() will perform in a predictable fashion and a plain string needs to be tested against that output then writing foo == stringToTest is a lot cleaner than foo.toString() === stringToTest.
@Alternatex If the point was clarity they shouldn't have made it TRIPLE equals! No beginner understands it. At least double equals is known from other languages. Also, there is no reasonable situation is a gross misstatement. Think about (native) Javascript types Number and String. Their existence prove the Javascript authors had certain use cases in mind for ==. Do you really think that new String('hi') === 'hi' evaluating to false is very clear? Please write a code snippet that tests your function argument against 'hi' accepting both String and string and tell me that is clear.
D
Daniel Vandersluis

JSLint is inherently more defensive than the Javascript syntax allows for.

From the JSLint documentation:

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. When comparing to any of the following values, use the === or !== operators (which do not do type coercion): 0 '' undefined null false true If you only care that a value is truthy or falsy, then use the short form. Instead of (foo != 0) just say (foo) and instead of (foo == 0) say (!foo) The === and !== operators are preferred.


I have to conclude the people from JSLint work in some very high ivory tower that they never get out of. Javascript was designed to be used with the == operator. The === is a special case... JSLint tries to make it seem like using == would somehow be wrong... However, try this: var x = 4, y = new Number(4); if (x == y) {alert('Javascript depends on == just embrace it!');}. Primitive types have corresponding classes that substitute for them (Number, String) and Javascript depends on the == operator to make comparing these natural.
J
Justin Niessner

Keep in mind that JSLint enforces one persons idea of what good JavaScript should be. You still have to use common sense when implementing the changes it suggests.

In general, comparing type and value will make your code safer (you will not run into the unexpected behavior when type conversion doesn't do what you think it should).


Plus it cannot be as context-smart as a programmer. It's just working on the basis that most users get tripped up by auto type conversion inherent in the system (like violence - "help help I'm being repressed!")
o
outis nihil

Triple-equal is different to double-equal because in addition to checking whether the two sides are the same value, triple-equal also checks that they are the same data type.

So ("4" == 4) is true, whereas ("4" === 4) is false.

Triple-equal also runs slightly quicker, because JavaScript doesn't have to waste time doing any type conversions prior to giving you the answer.

JSLint is deliberately aimed at making your JavaScript code as strict as possible, with the aim of reducing obscure bugs. It highlights this sort of thing to try to get you to code in a way that forces you to respect data types.

But the good thing about JSLint is that it is just a guide. As they say on the site, it will hurt your feelings, even if you're a very good JavaScript programmer. But you shouldn't feel obliged to follow its advice. If you've read what it has to say and you understand it, but you are sure your code isn't going to break, then there's no compulsion on you to change anything.

You can even tell JSLint to ignore categories of checks if you don't want to be bombarded with warnings that you're not going to do anything about.


I did not ask "What is ===", so I am not sure why you answered it.
@Metropolis: if for no other reason, then as background in case someone else read the answer who didn't know. I did try to answer your question in the paragraphs after that, though.
@Spudley + 1 for the additional and useful information
yep, it's 10-100 times faster: jsperf speed test
L
Lekensteyn

A quote from http://javascript.crockford.com/code.html:

=== and !== Operators. It is almost always better to use the === and !== operators. The == and != operators do type coercion. In particular, do not use == to compare against falsy values.

JSLint is very strict, their 'webjslint.js' does not even pass their own validation.


Nice clarification. That's true, about webjslint.js not validating--though most of the errors I see right now have to do with spacing. Clearly, one must use common sense and reasonable judgment when reviewing JavaScript using JSLint.
The use of the word always automatically disqualifies this quote as wisdom. Smart programmers aren't dogmatic. They use what is best in the given situation. And they welcome and embrace any tool built in to the very core of the language, not just dismiss it with a just never touch it. Bottom line: My code is shorter (and not just from saving one = character), thus my site loads faster, at less bandwidth cost, thus my user is better served.
n
nano2nd

If you want to test for falsyness. JSLint does not allow

if (foo == null)

but does allow

if (!foo)

Use ===, which JSLint recommends.
@NarawaGames This solution is perfectly acceptable.
This answer is not good. The thing is that both of these mean something else. foo == null checks for null or undefined. !foo checks for null, undefined, 0 and empty string.
@Markos This answer is meant to be a helpful alternative to make JSLint happy and keep your code logic intact, not to be an exact equivalent. This is why I prefixed the answer with "If checking for falsyness"
E
EM-Creations

To help explain this question and also explain why NetBeans (from) 7.3 has started showing this warning this is an extract from the response on the NetBeans bug tracker when someone reported this as a bug:

It is good practice to use === rather than == in JavaScript.

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. JSLint cannot reliably determine if == is being used correctly, so it is best to not use == and != at all and to always use the more reliable === and !== operators instead.

Reference


I found this error just now using Netbeans as well. It is strange that they would treat this with warning severity due to the bizarre example case they provided.
i mean, it is true, but there are many use cases in which the person knows that the two compared things will be of the same type, so it seems strange that due to this strange case where a carriage return might be compared to the number zero is the reason all usages of == are considered wrong. i am finding out though that === is faster though, since no type conversions are done. i'm surprised i didn't find this out before netbeans.
@oMiKeY Yes I see what you mean, they could have given more realistic examples!
R
Rushyo

Well it can't really cause problems, it's just giving you advice. Take it or leave it. That said, I'm not sure how clever it is. There may well be contexts in which it doesn't present it as an issue.


but why is the word "Expected" used? That makes it sound like you should always do this.
The evaluator is looking for a valid response, as it's trying to validate it. If it doesn't get a valid response, then it's not what it expected. The validator starts with the assumption that everything is fine and then points out errors as it traverses the code. It doesn't necessarily understand what is an invalid response, it just knows when it sees a non-valid one. It could also work in the reverse, deliberately searching for bad code according to rules of what is bad. Whitelisting vs blacklisting.
The question was "Does that really make any sense?". Given that question, yes, it does. Also, it was five years ago. Jesus.