]> git.proxmox.com Git - pve-eslint.git/blob - eslint/docs/rules/no-redeclare.md
c3b8422cf3f68ae0e71786d421b4095c8f68d730
[pve-eslint.git] / eslint / docs / rules / no-redeclare.md
1 # disallow variable redeclaration (no-redeclare)
2
3 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.
4
5 ## Rule Details
6
7 This rule is aimed at eliminating variables that have multiple declarations in the same scope.
8
9 Examples of **incorrect** code for this rule:
10
11 ```js
12 /*eslint no-redeclare: "error"*/
13
14 var a = 3;
15 var a = 10;
16 ```
17
18 Examples of **correct** code for this rule:
19
20 ```js
21 /*eslint no-redeclare: "error"*/
22
23 var a = 3;
24 // ...
25 a = 10;
26 ```
27
28 ## Options
29
30 This rule takes one optional argument, an object with a boolean property `"builtinGlobals"`. It defaults to `true`.
31 If set to `true`, this rule also checks redeclaration of built-in globals, such as `Object`, `Array`, `Number`...
32
33 ### builtinGlobals
34
35 The `"builtinGlobals"` option will check for redeclaration of built-in globals in global scope.
36
37 Examples of **incorrect** code for the `{ "builtinGlobals": true }` option:
38
39 ```js
40 /*eslint no-redeclare: ["error", { "builtinGlobals": true }]*/
41
42 var Object = 0;
43 ```
44
45 Examples of **incorrect** code for the `{ "builtinGlobals": true }` option and the `browser` environment:
46
47 ```js
48 /*eslint no-redeclare: ["error", { "builtinGlobals": true }]*/
49 /*eslint-env browser*/
50
51 var top = 0;
52 ```
53
54 The `browser` environment has many built-in global variables (for example, `top`). Some of built-in global variables cannot be redeclared.
55
56 Note that when using the `node` or `commonjs` environments (or `ecmaFeatures.globalReturn`, if using the default parser), the top scope of a program is not actually the global scope, but rather a "module" scope. When this is the case, declaring a variable named after a builtin global is not a redeclaration, but rather a shadowing of the global variable. In that case, the [`no-shadow`](no-shadow.md) rule with the `"builtinGlobals"` option should be used.
57
58 ## Related Rules
59
60 * [no-shadow](no-shadow.md)