Disallow Redeclaring Variables (no-redeclare)

In JavaScript, it’s possible to redeclare the same variable name using var. This can lead to confusion as to where the variable is actually declared and initialized.

Rule Details

This rule is aimed at eliminating variables that have multiple declarations in the same scope.

The following patterns are considered problems:

/*eslint no-redeclare: 2*/

var a = 3;
var a = 10; /*error "a" is already defined*/

The following patterns are not considered problems:

/*eslint no-redeclare: 2*/

var a = 3;
// ...
a = 10;

Options

This rule takes one option, an object, with a property "builtinGlobals".

{
    "no-redeclare": [2, {"builtinGlobals": true}]
}

builtinGlobals

false by default. If this is true, this rule checks with built-in global variables such as Object, Array, Number, …

When {"builtinGlobals": true}, the following patterns are considered problems:

/*eslint no-redeclare: [2, { "builtinGlobals": true }]*/

var Object = 0; /*error "Object" is already defined*/

When {"builtinGlobals": true} and under browser environment, the following patterns are considered problems:

/*eslint-env browser*/
/*eslint no-redeclare: [2, { "builtinGlobals": true }]*/

var top = 0; /*error "top" is already defined*/

Version

This rule was introduced in ESLint 0.0.9.

Resources