{a} is already defined

History

This warning has existed in two forms across JSLint and JSHint. It was introduced in the original version of JSLint and has remained in both tools ever since.

  • In JSLint releases prior to May 2015 and all versions of JSHint the warning given is "{a} is already defined"

  • In JSLint releases from May 2015 and later the warning is "Redefinition of '{a}' from line {b}"

The situations that produce the warning have not changed despite changes to the text of the warning itself.

When do I get this error?

The "{a} is already defined" error is thrown when JSLint or JSHint encounters a declaration with an identifier that has been used in a previous declaration. This applies to both variable and function declarations. However it only applies when the declarations appear within the scope of a function rather than the global scope. In the following example we attempt to declare the variable x twice within the same scope:

function test() {
    "use strict";
    var a = 1,
        a = 2;
    return a;
}

Why do I get this error?

This error is raised to highlight a probable bug. Your code will most likely fail to work as expected if you do not resolve this issue.

Since variable and function declarations are hoisted to the top of the scope in which they occur the above code is effectively interpreted as a single declaration followed by two assignments. The variable will retain the value of the final assignment.

If your code is similar to the above then it's likely you have mistyped the identifier of one of the variables. There are no situations in which it makes sense or is useful to redeclare a variable. Simply rename one of them appropriately:

function test() {
    "use strict";
    var a = 1,
        b = 2;
}

Alternatively, you may have intended to have two assignments and simply mistyped a semicolon as a comma. In that case it's also an obvious fix:

function test() {
    "use strict";
    var a = 1;
    a = 2;
}

In JSHint 1.0.0 and above you have the ability to ignore any warning with a special option syntax. The identifier of this warning is W004. This means you can tell JSHint to not issue this warning with the /*jshint -W004 */ directive.


About the author

James Allardice

This article was written by James Allardice, Software engineer at Tesco and orangejellyfish in London. Passionate about React, Node and writing clean and maintainable JavaScript. Uses linters (currently ESLint) every day to help achieve this.