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.

, , ,
Trackback

2 comments untill now

  1. [...] {a} ({b}% scanned) ‘{a}’ is a function. ‘{a}’ is a statement label. ‘{a}’ is already defined. ‘{a}’ is better written without quotes. (removed) ‘{a}’ is not a label. [...]

  2. Thanks for the explanation…I am one of the developers who has been assuming wrong and just decide to finally figure out what that warning is about!

Add your comment now