Skip to content

Commit b126016

Browse files
author
Aniruddh Agarwal
committed
Add section on variable assignment chaining
1 parent e92eaef commit b126016

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1417,6 +1417,38 @@ Other Style Guides
14171417
return name;
14181418
}
14191419
```
1420+
<a name=variables--no-chain-assignment""></a><a name="13.5"></a>
1421+
- [13.5](#variables--no-chain-assignment) Don't chain variable assignments. eslint: [`one-var`](http://eslint.org/docs/rules/one-var.html)
1422+
1423+
> Why? Chaining variable assignments creates implicit global variables.
1424+
1425+
```javascript
1426+
// bad
1427+
(function example() {
1428+
// JavaScript interprets this as
1429+
// let a = ( b = ( c = 1 ) );
1430+
// The let keyword only applies to variable a; variables b and c become
1431+
// global variables.
1432+
let a = b = c = 1;
1433+
}());
1434+
1435+
console.log(a); // undefined
1436+
console.log(b); // 1
1437+
console.log(c); // 1
1438+
1439+
// good
1440+
(function example() {
1441+
let a = 1;
1442+
let b = a;
1443+
let c = a;
1444+
}());
1445+
1446+
console.log(a); // undefined
1447+
console.log(b); // undefined
1448+
console.log(c); // undefined
1449+
1450+
// the same applies for `const`
1451+
```
14201452
14211453
**[⬆ back to top](#table-of-contents)**
14221454

0 commit comments

Comments
 (0)