Skip to content

Commit 48b53d6

Browse files
authoredMar 20, 2025··
docs: replace var with const in examples (#19539)
* docs: replace var with const in examples * docs: update examples no-shadow
1 parent 0e20aa7 commit 48b53d6

5 files changed

+44
-44
lines changed
 

‎docs/src/rules/no-shadow-restricted-names.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ further_reading:
1313
ES5 §15.1.1 Value Properties of the Global Object (`NaN`, `Infinity`, `undefined`) as well as strict mode restricted identifiers `eval` and `arguments` are considered to be restricted names in JavaScript. Defining them to mean something else can have unintended consequences and confuse others reading the code. For example, there's nothing preventing you from writing:
1414

1515
```js
16-
var undefined = "foo";
16+
const undefined = "foo";
1717
```
1818

1919
Then any code used within the same scope would not get the global `undefined`, but rather the local version with a very different meaning.
@@ -31,7 +31,7 @@ function NaN(){}
3131

3232
!function(Infinity){};
3333

34-
var undefined = 5;
34+
const undefined = 5;
3535

3636
try {} catch(eval){}
3737
```
@@ -59,12 +59,12 @@ Examples of **correct** code for this rule:
5959
```js
6060
/*eslint no-shadow-restricted-names: "error"*/
6161

62-
var Object;
62+
let Object;
6363

6464
function f(a, b){}
6565

6666
// Exception: `undefined` may be shadowed if the variable is never assigned a value.
67-
var undefined;
67+
let undefined;
6868
```
6969

7070
:::

‎docs/src/rules/no-shadow.md

+23-23
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ further_reading:
1111
Shadowing is the process by which a local variable shares the same name as a variable in its containing scope. For example:
1212

1313
```js
14-
var a = 3;
14+
const a = 3;
1515
function b() {
16-
var a = 10;
16+
const a = 10;
1717
}
1818
```
1919

@@ -30,13 +30,13 @@ Examples of **incorrect** code for this rule:
3030
```js
3131
/*eslint no-shadow: "error"*/
3232

33-
var a = 3;
33+
const a = 3;
3434
function b() {
35-
var a = 10;
35+
const a = 10;
3636
}
3737

38-
var c = function () {
39-
var a = 10;
38+
const c = function () {
39+
const a = 10;
4040
}
4141

4242
function d(a) {
@@ -45,7 +45,7 @@ function d(a) {
4545
d(a);
4646

4747
if (true) {
48-
let a = 5;
48+
const a = 5;
4949
}
5050
```
5151

@@ -74,7 +74,7 @@ Examples of **incorrect** code for the `{ "builtinGlobals": true }` option:
7474
/*eslint no-shadow: ["error", { "builtinGlobals": true }]*/
7575

7676
function foo() {
77-
var Object = 0;
77+
const Object = 0;
7878
}
7979
```
8080

@@ -98,15 +98,15 @@ Examples of **incorrect** code for the default `{ "hoist": "functions" }` option
9898
/*eslint no-shadow: ["error", { "hoist": "functions" }]*/
9999

100100
if (true) {
101-
let b = 6;
101+
const b = 6;
102102
}
103103

104104
function b() {}
105105
```
106106

107107
:::
108108

109-
Although `let b` in the `if` statement is before the *function* declaration in the outer scope, it is incorrect.
109+
Although `const b` in the `if` statement is before the *function* declaration in the outer scope, it is incorrect.
110110

111111
Examples of **correct** code for the default `{ "hoist": "functions" }` option:
112112

@@ -116,15 +116,15 @@ Examples of **correct** code for the default `{ "hoist": "functions" }` option:
116116
/*eslint no-shadow: ["error", { "hoist": "functions" }]*/
117117

118118
if (true) {
119-
let a = 3;
119+
const a = 3;
120120
}
121121

122-
let a = 5;
122+
const a = 5;
123123
```
124124

125125
:::
126126

127-
Because `let a` in the `if` statement is before the *variable* declaration in the outer scope, it is correct.
127+
Because `const a` in the `if` statement is before the *variable* declaration in the outer scope, it is correct.
128128

129129
#### hoist: all
130130

@@ -136,11 +136,11 @@ Examples of **incorrect** code for the `{ "hoist": "all" }` option:
136136
/*eslint no-shadow: ["error", { "hoist": "all" }]*/
137137

138138
if (true) {
139-
let a = 3;
140-
let b = 6;
139+
const a = 3;
140+
const b = 6;
141141
}
142142

143-
let a = 5;
143+
const a = 5;
144144
function b() {}
145145
```
146146

@@ -156,17 +156,17 @@ Examples of **correct** code for the `{ "hoist": "never" }` option:
156156
/*eslint no-shadow: ["error", { "hoist": "never" }]*/
157157

158158
if (true) {
159-
let a = 3;
160-
let b = 6;
159+
const a = 3;
160+
const b = 6;
161161
}
162162

163-
let a = 5;
163+
const a = 5;
164164
function b() {}
165165
```
166166

167167
:::
168168

169-
Because `let a` and `let b` in the `if` statement are before the declarations in the outer scope, they are correct.
169+
Because `const a` and `const b` in the `if` statement are before the declarations in the outer scope, they are correct.
170170

171171
### allow
172172

@@ -207,7 +207,7 @@ Examples of **incorrect** code for the `{ "ignoreOnInitialization": "true" }` op
207207
```js
208208
/*eslint no-shadow: ["error", { "ignoreOnInitialization": true }]*/
209209

210-
var x = x => x;
210+
const x = x => x;
211211
```
212212

213213
:::
@@ -221,9 +221,9 @@ Examples of **correct** code for the `{ "ignoreOnInitialization": true }` option
221221
```js
222222
/*eslint no-shadow: ["error", { "ignoreOnInitialization": true }]*/
223223

224-
var x = foo(x => x)
224+
const x = foo(x => x)
225225

226-
var y = (y => y)()
226+
const y = (y => y)()
227227
```
228228

229229
:::

‎docs/src/rules/no-throw-literal.md

+6-6
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,12 @@ throw undefined;
2828

2929
throw null;
3030

31-
var err = new Error();
31+
const err = new Error();
3232
throw "an " + err;
3333
// err is recast to a string literal
3434

35-
var err = new Error();
36-
throw `${err}`
35+
const er2 = new Error();
36+
throw `${err2}`
3737

3838
```
3939

@@ -50,7 +50,7 @@ throw new Error();
5050

5151
throw new Error("error");
5252

53-
var e = new Error("error");
53+
const e = new Error("error");
5454
throw e;
5555

5656
try {
@@ -73,7 +73,7 @@ Examples of **correct** code for this rule, but which do not throw an `Error` ob
7373
```js
7474
/*eslint no-throw-literal: "error"*/
7575

76-
var err = "error";
76+
const err = "error";
7777
throw err;
7878

7979
function foo(bar) {
@@ -83,7 +83,7 @@ throw foo("error");
8383

8484
throw new String("error");
8585

86-
var baz = {
86+
const baz = {
8787
bar: "error"
8888
};
8989
throw baz.bar;

‎docs/src/rules/no-underscore-dangle.md

+10-10
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ rule_type: suggestion
66
As far as naming conventions for identifiers go, dangling underscores may be the most polarizing in JavaScript. Dangling underscores are underscores at either the beginning or end of an identifier, such as:
77

88
```js
9-
var _foo;
9+
let _foo;
1010
```
1111

1212
There is a long history of marking "private" members with dangling underscores in JavaScript, beginning with SpiderMonkey adding nonstandard methods such as `__defineGetter__()`. Since that time, using a single underscore prefix has become the most popular convention for indicating a member is not part of the public interface of an object.
@@ -26,8 +26,8 @@ Examples of **incorrect** code for this rule:
2626
```js
2727
/*eslint no-underscore-dangle: "error"*/
2828

29-
var foo_;
30-
var __proto__ = {};
29+
let foo_;
30+
const __proto__ = {};
3131
foo._bar();
3232
```
3333

@@ -40,10 +40,10 @@ Examples of **correct** code for this rule:
4040
```js
4141
/*eslint no-underscore-dangle: "error"*/
4242

43-
var _ = require('underscore');
44-
var obj = _.contains(items, item);
43+
const _ = require('underscore');
44+
const obj = _.contains(items, item);
4545
obj.__proto__ = {};
46-
var file = __filename;
46+
const file = __filename;
4747
function foo(_bar) {};
4848
const bar = { onClick(_bar) {} };
4949
const baz = (_bar) => {};
@@ -74,7 +74,7 @@ Examples of additional **correct** code for this rule with the `{ "allow": ["foo
7474
```js
7575
/*eslint no-underscore-dangle: ["error", { "allow": ["foo_", "_bar"] }]*/
7676

77-
var foo_;
77+
let foo_;
7878
foo._bar();
7979
```
8080

@@ -89,7 +89,7 @@ Examples of **correct** code for this rule with the `{ "allowAfterThis": true }`
8989
```js
9090
/*eslint no-underscore-dangle: ["error", { "allowAfterThis": true }]*/
9191

92-
var a = this.foo_;
92+
const a = this.foo_;
9393
this._bar();
9494
```
9595

@@ -106,7 +106,7 @@ Examples of **correct** code for this rule with the `{ "allowAfterSuper": true }
106106

107107
class Foo extends Bar {
108108
doSomething() {
109-
var a = super.foo_;
109+
const a = super.foo_;
110110
super._bar();
111111
}
112112
}
@@ -123,7 +123,7 @@ Examples of **correct** code for this rule with the `{ "allowAfterThisConstructo
123123
```js
124124
/*eslint no-underscore-dangle: ["error", { "allowAfterThisConstructor": true }]*/
125125

126-
var a = this.constructor.foo_;
126+
const a = this.constructor.foo_;
127127
this.constructor._bar();
128128
```
129129

‎docs/src/rules/strict.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ function foo() {
210210
}
211211
}());
212212

213-
var foo = (function() {
213+
const foo2 = (function() {
214214
"use strict";
215215

216216
return function foo(a = 1) {

0 commit comments

Comments
 (0)
Please sign in to comment.