In languages where compilation of code is required up-front before it can be executed, the developer often has the luxury of being warned about unused or unreachable code by the compiler.
For JavaScript, we have no such luxury, however, JSLint is there to emulate some of the features that a compiler would provide.
So, when we write code that contains unused variables, JSLint will helpfully warn about us about it. The following code sample will yeild this error:
function MyFunc(){
var x = 0;
for (x=0; x<0; x++){
//do something
var y = 0;
}
}
At best this is redundant code that was never cleared up and, at worst, this is some sort of bug that is yet to be identified.
A Guide To JSLint Messages
This article is one of a series on the error and warning messages produced by JSLint.
JavaScript, javascript-variables, jslint, jslint-messages
JavaScript isn’t very particular about whether or not you declare your variables (its loosely typed). In fact, you can often get away without doing so. Consider the following:
x = 0;
alert(x); //alerts '0' quite happily
The about example isn’t overly problematic, but that doesn’t mean that its a sensible thing to do. This, assumes you intended to do this. Consider the following:
var receivedParts = 0;
recievedParts = GetReceivedParts();
Whoops! Can you spot what’s wrong? Yes, we’ve spelled ‘received’ differently (mixed the ‘e’ and ‘i’ in our typing). JavaScript doesn’t care, though.A value is assigned to the latter without complaint.
Of course we might have intended to do this, but even this is inadvisable. Consider the following:
receivedParts = receivedParts + 1;
Our variable isn’t defined, so, again we have a bug.
Of course, in strongly typed languages, this isn’t an issue. In effect, JSLint is attempting to make JavaScript a more strongly typed language.
A Guide To JSLint Messages
This article is one of a series on the error and warning messages produced by JSLint.
JavaScript, javascript-variables, jslint, jslint-messages
JavaScript will let you repeat your declarations without complaint, however it’s rarely, if ever, a good idea to do so:
var x = 0;
var x = 1; //no complaints!
JavaScript here will simply disregard the first declaration. The above is clearly nonsense, but there are circumstances in which a developer might declare a variable twice in faith. Consider the following:
if (condition) {
var x = 0;
} else {
var x = 1;
}
Here, the developer has made the assumption that JavaScript allows you to scope variables locally. The developer is, of course, wrong in this assumption. All declarations are hoisted to the top of the containing scope. So, in effect we still have two declarations.
The same message is produced for other declarations, e.g. functions:
function foo(){};
function foo(){}; //'foo' is already defined.
Multiple declarations of the same name are subject to confusion and buggy code, and therefore should be avoided.
A Guide To JSLint Messages
This article is one of a series on the error and warning messages produced by JSLint.
JavaScript, javascript-variables, jslint, jslint-messages