1

I am using this code in JSLint

var string = "Hello";
if (string == "Hello") {
    string = "World";
}

And I am getting this error

Expected '===' and instead saw '=='.
if (string == "Hello") {

Why JSLint is throwing this error?

kiranvj
  • 32,342
  • 7
  • 71
  • 76

2 Answers2

2

From JSLint docs under == 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. 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.

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)

There is an eqeq option that allows the use of == and !=.

How to tell JSLint not to throw this error.

Use JSLint directive eqeq

like this

/*jslint eqeq: true*/

So the full code will look like

/*jslint eqeq: true*/
var string = "Hello";
if (string == "Hello") {
    string = "World";
}
kiranvj
  • 32,342
  • 7
  • 71
  • 76
0

Because JSLint thinks == should never be used (the equality system is kind of weird).

My advice is to learn how == and === work, and how they compare (check the specification). Then, you can make the best decision yourself instead of simply tagging a language construct with never use it.

alex
  • 479,566
  • 201
  • 878
  • 984