]> git.proxmox.com Git - pve-eslint.git/blob - eslint/docs/rules/no-const-assign.md
first commit
[pve-eslint.git] / eslint / docs / rules / no-const-assign.md
1 # Disallow modifying variables that are declared using `const` (no-const-assign)
2
3 We cannot modify variables that are declared using `const` keyword.
4 It will raise a runtime error.
5
6 Under non ES2015 environment, it might be ignored merely.
7
8 ## Rule Details
9
10 This rule is aimed to flag modifying variables that are declared using `const` keyword.
11
12 Examples of **incorrect** code for this rule:
13
14 ```js
15 /*eslint no-const-assign: "error"*/
16 /*eslint-env es6*/
17
18 const a = 0;
19 a = 1;
20 ```
21
22 ```js
23 /*eslint no-const-assign: "error"*/
24 /*eslint-env es6*/
25
26 const a = 0;
27 a += 1;
28 ```
29
30 ```js
31 /*eslint no-const-assign: "error"*/
32 /*eslint-env es6*/
33
34 const a = 0;
35 ++a;
36 ```
37
38 Examples of **correct** code for this rule:
39
40 ```js
41 /*eslint no-const-assign: "error"*/
42 /*eslint-env es6*/
43
44 const a = 0;
45 console.log(a);
46 ```
47
48 ```js
49 /*eslint no-const-assign: "error"*/
50 /*eslint-env es6*/
51
52 for (const a in [1, 2, 3]) { // `a` is re-defined (not modified) on each loop step.
53 console.log(a);
54 }
55 ```
56
57 ```js
58 /*eslint no-const-assign: "error"*/
59 /*eslint-env es6*/
60
61 for (const a of [1, 2, 3]) { // `a` is re-defined (not modified) on each loop step.
62 console.log(a);
63 }
64 ```
65
66 ## When Not To Use It
67
68 If you don't want to be notified about modifying variables that are declared using `const` keyword, you can safely disable this rule.