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