Saturday, November 7, 2020

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

Triple-equal is different to double-equal because in addition to checking whether the two sides are the same value, triple-equal also checks that they are the same data type.


So ("4" == 4) is true, whereas ("4" === 4) is false.


Triple-equal also runs slightly quicker, because JavaScript doesn't have to waste time doing any type conversions prior to giving you the answer.


JSLint is deliberately aimed at making your JavaScript code as strict as possible, with the aim of reducing obscure bugs. It highlights this sort of thing to try to get you to code in a way that forces you to respect data types.


But the good thing about JSLint is that it is just a guide. As they say on the site, it will hurt your feelings, even if you're a very good JavaScript programmer. But you shouldn't feel obliged to follow its advice. If you've read what it has to say and you understand it, but you are sure your code isn't going to break, then there's no compulsion on you to change anything.


You can even tell JSLint to ignore categories of checks if you don't want to be bombarded with warnings that you're not going to do anything about.



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.


References:

https://stackoverflow.com/questions/3735939/jslint-expected-and-instead-saw


No comments:

Post a Comment