简体   繁体   中英

Comparison operation - Correct Syntax?

Coming from PHP I understand the difference in using == verses === in a comparison operation, but with JavaScript is it acceptable to use == or ===?

I wanted to validate my JavaScript code and found JSLint . While running the validation with JSLint I see these types of messages.

Some examples:

var show = $('input[name=female]:checked').val() == "true";
  • Problem at line 278 character 70: Expected '!==' and instead saw '!='.

and

var show = $('input[name=living]:checked').val() != "false";
  • Problem at line 283 character 38: Expected '!==' and instead saw '!='.

So My question is, Would using the current syntax of == or != be valid/correct or do I need to use === and !== ?

EDIT: Just to point out the code does work

It is correct in a way but JSLint expects strict comparison to prevent problems with type conversion.

It is good practice to always use strict comparison and "normal" comparison only where you really need/want it (because you want to make use of type conversion for some reason).

From JSLint's documentation :

== and !=

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.

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) 

Always use the === and !== operators.

Crockford and JSLint are somewhat dogmatic on this point, and I think over-prescriptive. Always using === rather than == is a reasonable rule of thumb for inexperienced JavaScript developers, but if you know the basic rules then there is no problem with using == . In particular, if the two operands are guaranteed to be of the same type (such as in a typeof comparison like typeof foo == "undefined" ) then the two operators are specified to follow precisely the same steps.

There is also a reasonable point about habitually using === removing the need to think about which operator to use, but frankly I prefer to be forced to consider all the possible values of the two operands I'm comparing and find JSLint unnecessarily nannying.

The == syntax is valid but in some cases it can lead to rather nasty bugs in large projects. If you don't have a good reason to use == you should by default write JavaScript with === because it checks for type as you know.

It works either way, but you will save yourself pain in the future if you default to ===.

Depends on the situation.

=== is often preferred because you won't have any false-positives that you might not have considered (it might also be slightly quicker in some implementations because it compares to less potential values, although I haven't checked that, just a thought, and the speed difference would be negligible).

== is often more convenient to cover many/all cases (if I'm not mistaken, jQuery doesn't pass JSLint because in some places the code intentionally uses == to cover a multitude of cases rather than testing each one individually).

你可以只做==和!=,我不认为会有任何问题。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM