You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Frontenders at Airbnb at have been discussing this change for a bit, and
we've come to favor one-var-per-variable over one-var-only declarations.
Two things improve the maintainability of this style over one var for
multiple variables:
1. You never have a diff of a line that's removing a `;` and inserting
a `,`.
2. You can't accidentally declare global variables because you have a
one-character mistake (semicolon instead of comma):
```javascript
var foo = 1,
bar = 2;
baz = 3; // added later and (accidentally) declared globally
```
Copy file name to clipboardExpand all lines: README.md
+44-28Lines changed: 44 additions & 28 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -50,8 +50,8 @@
50
50
+`undefined`
51
51
52
52
```javascript
53
-
var foo =1,
54
-
bar = foo;
53
+
var foo =1;
54
+
var bar = foo;
55
55
56
56
bar =9;
57
57
@@ -64,8 +64,8 @@
64
64
+`function`
65
65
66
66
```javascript
67
-
var foo = [1, 2],
68
-
bar = foo;
67
+
var foo = [1, 2];
68
+
var bar = foo;
69
69
70
70
bar[0] = 9;
71
71
@@ -151,9 +151,9 @@
151
151
- When you need to copy an array use Array#slice. [jsPerf](http://jsperf.com/converting-arguments-to-an-array/7)
152
152
153
153
```javascript
154
-
var len = items.length,
155
-
itemsCopy = [],
156
-
i;
154
+
var len = items.length;
155
+
var itemsCopy = [];
156
+
var i;
157
157
158
158
// bad
159
159
for (i = 0; i < len; i++) {
@@ -216,10 +216,10 @@
216
216
- When programmatically building up a string, use Array#join instead of string concatenation. Mostly for IE: [jsPerf](http://jsperf.com/string-vs-array-concat/2).
217
217
218
218
```javascript
219
-
var items,
220
-
messages,
221
-
length,
222
-
i;
219
+
var items;
220
+
var messages;
221
+
var length;
222
+
var i;
223
223
224
224
messages = [{
225
225
state: 'success',
@@ -366,17 +366,28 @@
366
366
var superPower =newSuperPower();
367
367
```
368
368
369
-
- Use one `var` declaration for multiple variables and declare each variable on a newline.
369
+
- Use one `var` declaration per variable.
370
370
371
371
```javascript
372
372
// bad
373
+
var items =getItems(),
374
+
goSportsTeam =true,
375
+
dragonball ='z';
376
+
377
+
// good
373
378
var items =getItems();
374
379
var goSportsTeam =true;
375
380
var dragonball ='z';
381
+
```
376
382
377
-
// good
383
+
It's easier to add new variable declarations this way, and you never have
384
+
to worry about swapping out a `;` for a `,` or introducing punctuation-only
385
+
diffs. Also, you can't make the following mistake:
386
+
387
+
```javascript
388
+
// can you catch the error?
378
389
var items =getItems(),
379
-
goSportsTeam =true,
390
+
goSportsTeam =true;
380
391
dragonball ='z';
381
392
```
382
393
@@ -389,17 +400,18 @@
389
400
goSportsTeam =true;
390
401
391
402
// bad
392
-
var i, items =getItems(),
393
-
dragonball,
394
-
goSportsTeam =true,
395
-
len;
403
+
var i;
404
+
var items =getItems();
405
+
var dragonball;
406
+
var goSportsTeam =true;
407
+
var len;
396
408
397
409
// good
398
-
var items =getItems(),
399
-
goSportsTeam =true,
400
-
dragonball,
401
-
length,
402
-
i;
410
+
var items =getItems();
411
+
vargoSportsTeam =true;
412
+
vardragonball;
413
+
varlength;
414
+
var i;
403
415
```
404
416
405
417
- Assign variables at the top of their scope. This helps avoid issues with variable declaration and assignment hoisting related issues.
0 commit comments