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

前端 未结 8 1843
我在风中等你
我在风中等你 2020-11-30 21:08

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 tha

相关标签:
8条回答
  • 2020-11-30 21:31

    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.

    0 讨论(0)
  • 2020-11-30 21:41

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

    if (foo == null)
    

    but does allow

    if (!foo)
    
    0 讨论(0)
  • 2020-11-30 21:42

    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
    }
    
    0 讨论(0)
  • 2020-11-30 21:44

    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.

    0 讨论(0)
  • 2020-11-30 21:44

    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

    0 讨论(0)
  • 2020-11-30 21:47

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

    0 讨论(0)
提交回复
热议问题