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

前端 未结 8 1906
我在风中等你
我在风中等你 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.

提交回复
热议问题