10-second guide to JSLint
Use === instead of == .
No ++ or — .
Use /*global*/ to declare variables, like this: /*global foo, bar */ .
Use /*globals*/ to declare a bunch of variables at once. These are things you use but do not define. For example, if you use the window object in your program, put /*globals window */ at the top. (This is just like declaring a global variable in C.)
If you forget to declare a function that is not part of another function (that is, a function that does not have its own var ) then the function will be global! JSLint will complain about all such functions because it is impossible to tell which functions are meant to be global and which should have been declared locally. If you know that a function should be global, you can fix this problem by putting “/*global myFunctionName */” before it. This also applies to variables.
You cannot have multiple var s in the same scope. Put them all on one line with commas between them: “var x = 1, y = 2;”. You can have multiple vars if they are in different scopes
So what exactly is JSLint? Literally, JSLint is a function that takes source code as input and returns a description of any syntax problems found.
At first glance, it looks like they do the same thing. But they don’t. There’s a huge difference between the two, so it’s important to understand when to use each one.
‘ == ‘ will evaluate to true when the values of its operands are equal, after doing any necessary type conversions. If it needs to convert between numbers and strings, for example, it will convert the string to a number before comparing them.
‘ === ‘ on the other hand will not do any type conversion. If the values are not the same type (and not both null or undefined), then it will immediately return false without converting anything.
This is very important because sometimes you might want to check that two values are really equal without any type coercion.
What’s the difference between ‘==’ and ‘===’, or between ‘function()’ and ‘function’?
The short answer: Everything.
typeof “12”; // “string” typeof String(“12”); // “object” typeof 12; // “number” typeof Number(12); // “object”
string (e.g., “hello”)
number (e.g., 3)
boolean (true or false)
function (a block of code that can be executed)
null (an empty value)
undefined (the absence of a value)
How Does It Work?
JSLint scans your code character by character looking for potential problems. For example, JSLint will alert you if you forget to include a semi-colon at
null means nothing. undefined means a variable has been declared but not assigned a value.
The typeof operator returns a string that tells the type of the operand.
The == operator will compare for equality after doing any necessary type conversions. The === operator will not do the conversion, so if two values are not the same type === will simply return false. Ex: 0 == false, but 0 !== false
A triple equal (===) is strict in its comparison and won’t coerce any values to be compared. A double equal (==) will try to convert data types to be compared
The difference between using var, let and const is really about how you want your variable to behave. If we want to use a variable anywhere in our application and not just in one block then we would use var. If we want to limit the scope of our variables to be inside of one block then they should be let or const depending on if we want to change them later or not.