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?
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?
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";
}
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.