Skip to content

Commit 1324af0

Browse files
authored
docs: replace var with let and const in rules docs (#19392)
1 parent 8b87e00 commit 1324af0

File tree

5 files changed

+47
-47
lines changed

5 files changed

+47
-47
lines changed

docs/src/rules/no-eval.md

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ further_reading:
1212
JavaScript's `eval()` function is potentially dangerous and is often misused. Using `eval()` on untrusted code can open a program up to several different injection attacks. The use of `eval()` in most contexts can be substituted for a better, alternative approach to a problem.
1313

1414
```js
15-
var obj = { x: "foo" },
15+
const obj = { x: "foo" },
1616
key = "x",
1717
value = eval("obj." + key);
1818
```
@@ -28,17 +28,17 @@ Examples of **incorrect** code for this rule:
2828
```js
2929
/*eslint no-eval: "error"*/
3030

31-
var obj = { x: "foo" },
31+
const obj = { x: "foo" },
3232
key = "x",
3333
value = eval("obj." + key);
3434

35-
(0, eval)("var a = 0");
35+
(0, eval)("const a = 0");
3636

37-
var foo = eval;
38-
foo("var a = 0");
37+
const foo = eval;
38+
foo("const a = 0");
3939

4040
// This `this` is the global object.
41-
this.eval("var a = 0");
41+
this.eval("const a = 0");
4242
```
4343

4444
:::
@@ -51,7 +51,7 @@ Example of additional **incorrect** code for this rule with `window` global vari
5151
/*eslint no-eval: "error"*/
5252
/*global window*/
5353

54-
window.eval("var a = 0");
54+
window.eval("const a = 0");
5555
```
5656

5757
:::
@@ -64,7 +64,7 @@ Example of additional **incorrect** code for this rule with `global` global vari
6464
/*eslint no-eval: "error"*/
6565
/*global global*/
6666

67-
global.eval("var a = 0");
67+
global.eval("const a = 0");
6868
```
6969

7070
:::
@@ -76,22 +76,22 @@ Examples of **correct** code for this rule:
7676
```js
7777
/*eslint no-eval: "error"*/
7878

79-
var obj = { x: "foo" },
79+
const obj = { x: "foo" },
8080
key = "x",
8181
value = obj[key];
8282

8383
class A {
8484
foo() {
8585
// This is a user-defined method.
86-
this.eval("var a = 0");
86+
this.eval("const a = 0");
8787
}
8888

8989
eval() {
9090
}
9191

9292
static {
9393
// This is a user-defined static method.
94-
this.eval("var a = 0");
94+
this.eval("const a = 0");
9595
}
9696

9797
static eval() {
@@ -121,7 +121,7 @@ Example of **incorrect** code for this rule with the `{"allowIndirect": true}` o
121121
```js
122122
/*eslint no-eval: ["error", {"allowIndirect": true} ]*/
123123

124-
var obj = { x: "foo" },
124+
const obj = { x: "foo" },
125125
key = "x",
126126
value = eval("obj." + key);
127127
```
@@ -135,12 +135,12 @@ Examples of **correct** code for this rule with the `{"allowIndirect": true}` op
135135
```js
136136
/*eslint no-eval: ["error", {"allowIndirect": true} ]*/
137137

138-
(0, eval)("var a = 0");
138+
(0, eval)("const a = 0");
139139

140-
var foo = eval;
141-
foo("var a = 0");
140+
const foo = eval;
141+
foo("const a = 0");
142142

143-
this.eval("var a = 0");
143+
this.eval("const a = 0");
144144
```
145145

146146
:::
@@ -151,7 +151,7 @@ this.eval("var a = 0");
151151
/*eslint no-eval: ["error", {"allowIndirect": true} ]*/
152152
/*global window*/
153153

154-
window.eval("var a = 0");
154+
window.eval("const a = 0");
155155
```
156156

157157
:::
@@ -162,7 +162,7 @@ window.eval("var a = 0");
162162
/*eslint no-eval: ["error", {"allowIndirect": true} ]*/
163163
/*global global*/
164164

165-
global.eval("var a = 0");
165+
global.eval("const a = 0");
166166
```
167167

168168
:::
@@ -176,13 +176,13 @@ global.eval("var a = 0");
176176
module.exports = function(eval) {
177177
// If the value of this `eval` is built-in `eval` function, this is a
178178
// call of direct `eval`.
179-
eval("var a = 0");
179+
eval("const a = 0");
180180
};
181181
```
182182

183183
* This rule cannot catch renaming the global object. Such as:
184184

185185
```js
186-
var foo = window;
187-
foo.eval("var a = 0");
186+
const foo = window;
187+
foo.eval("const a = 0");
188188
```

docs/src/rules/no-nested-ternary.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ related_rules:
1010
Nesting ternary expressions can make code more difficult to understand.
1111

1212
```js
13-
var foo = bar ? baz : qux === quxx ? bing : bam;
13+
const foo = bar ? baz : qux === quxx ? bing : bam;
1414
```
1515

1616
## Rule Details
@@ -24,7 +24,7 @@ Examples of **incorrect** code for this rule:
2424
```js
2525
/*eslint no-nested-ternary: "error"*/
2626

27-
var thing = foo ? bar : baz === qux ? quxx : foobar;
27+
const thing = foo ? bar : baz === qux ? quxx : foobar;
2828

2929
foo ? baz === qux ? quxx() : foobar() : bar();
3030
```
@@ -38,16 +38,16 @@ Examples of **correct** code for this rule:
3838
```js
3939
/*eslint no-nested-ternary: "error"*/
4040

41-
var thing = foo ? bar : foobar;
41+
const thing = foo ? bar : foobar;
4242

43-
var thing;
43+
let otherThing;
4444

4545
if (foo) {
46-
thing = bar;
46+
otherThing = bar;
4747
} else if (baz === qux) {
48-
thing = quxx;
48+
otherThing = quxx;
4949
} else {
50-
thing = foobar;
50+
otherThing = foobar;
5151
}
5252
```
5353

docs/src/rules/no-ternary.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ related_rules:
1010
The ternary operator is used to conditionally assign a value to a variable. Some believe that the use of ternary operators leads to unclear code.
1111

1212
```js
13-
var foo = isBar ? baz : qux;
13+
const foo = isBar ? baz : qux;
1414
```
1515

1616
## Rule Details
@@ -24,7 +24,7 @@ Examples of **incorrect** code for this rule:
2424
```js
2525
/*eslint no-ternary: "error"*/
2626

27-
var foo = isBar ? baz : qux;
27+
const foo = isBar ? baz : qux;
2828

2929
function quux() {
3030
return foo ? bar() : baz();
@@ -40,7 +40,7 @@ Examples of **correct** code for this rule:
4040
```js
4141
/*eslint no-ternary: "error"*/
4242

43-
var foo;
43+
let foo;
4444

4545
if (isBar) {
4646
foo = baz;

docs/src/rules/no-undefined.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ The `undefined` variable in JavaScript is actually a property of the global obje
1717

1818
```js
1919
function doSomething(data) {
20-
var undefined = "hi";
20+
const undefined = "hi";
2121

2222
// doesn't do what you think it does
2323
if (data === undefined) {
@@ -46,9 +46,9 @@ Examples of **incorrect** code for this rule:
4646
```js
4747
/*eslint no-undefined: "error"*/
4848

49-
var foo = undefined;
49+
const foo = undefined;
5050

51-
var undefined = "foo";
51+
const undefined = "foo";
5252

5353
if (foo === undefined) {
5454
// ...
@@ -70,9 +70,9 @@ Examples of **correct** code for this rule:
7070
```js
7171
/*eslint no-undefined: "error"*/
7272

73-
var foo = void 0;
73+
const foo = void 0;
7474

75-
var Undefined = "foo";
75+
const Undefined = "foo";
7676

7777
if (typeof foo === "undefined") {
7878
// ...

docs/src/rules/no-unneeded-ternary.md

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,16 @@ Here are some examples:
1313

1414
```js
1515
// Bad
16-
var isYes = answer === 1 ? true : false;
16+
const isYes = answer === 1 ? true : false;
1717

1818
// Good
19-
var isYes = answer === 1;
19+
const isYes = answer === 1;
2020

2121
// Bad
22-
var isNo = answer === 1 ? false : true;
22+
const isNo = answer === 1 ? false : true;
2323

2424
// Good
25-
var isNo = answer !== 1;
25+
const isNo = answer !== 1;
2626
```
2727

2828
Another common mistake is using a single variable as both the conditional test and the consequent. In such cases, the logical `OR` can be used to provide the same functionality.
@@ -47,9 +47,9 @@ Examples of **incorrect** code for this rule:
4747
```js
4848
/*eslint no-unneeded-ternary: "error"*/
4949

50-
var a = x === 2 ? true : false;
50+
const a = x === 2 ? true : false;
5151

52-
var a = x ? true : false;
52+
const b = x ? true : false;
5353
```
5454

5555
:::
@@ -61,13 +61,13 @@ Examples of **correct** code for this rule:
6161
```js
6262
/*eslint no-unneeded-ternary: "error"*/
6363

64-
var a = x === 2 ? "Yes" : "No";
64+
const a = x === 2 ? "Yes" : "No";
6565

66-
var a = x !== false;
66+
const b = x !== false;
6767

68-
var a = x ? "Yes" : "No";
68+
const c = x ? "Yes" : "No";
6969

70-
var a = x ? y : x;
70+
const d = x ? y : x;
7171

7272
f(x ? x : 1); // default assignment - would be disallowed if defaultAssignment option set to false. See option details below.
7373
```
@@ -92,7 +92,7 @@ Examples of additional **incorrect** code for this rule with the `{ "defaultAssi
9292
```js
9393
/*eslint no-unneeded-ternary: ["error", { "defaultAssignment": false }]*/
9494

95-
var a = x ? x : 1;
95+
const a = x ? x : 1;
9696

9797
f(x ? x : 1);
9898
```

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy