Skip to content

Commit 8d51bd5

Browse files
author
Jake Teton-Landis
committed
Switch from single var to one-var-per-variable
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 ```
1 parent bae9bc6 commit 8d51bd5

File tree

1 file changed

+44
-28
lines changed

1 file changed

+44
-28
lines changed

README.md

Lines changed: 44 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@
5050
+ `undefined`
5151

5252
```javascript
53-
var foo = 1,
54-
bar = foo;
53+
var foo = 1;
54+
var bar = foo;
5555

5656
bar = 9;
5757

@@ -64,8 +64,8 @@
6464
+ `function`
6565

6666
```javascript
67-
var foo = [1, 2],
68-
bar = foo;
67+
var foo = [1, 2];
68+
var bar = foo;
6969
7070
bar[0] = 9;
7171
@@ -151,9 +151,9 @@
151151
- When you need to copy an array use Array#slice. [jsPerf](http://jsperf.com/converting-arguments-to-an-array/7)
152152
153153
```javascript
154-
var len = items.length,
155-
itemsCopy = [],
156-
i;
154+
var len = items.length;
155+
var itemsCopy = [];
156+
var i;
157157
158158
// bad
159159
for (i = 0; i < len; i++) {
@@ -216,10 +216,10 @@
216216
- 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).
217217
218218
```javascript
219-
var items,
220-
messages,
221-
length,
222-
i;
219+
var items;
220+
var messages;
221+
var length;
222+
var i;
223223
224224
messages = [{
225225
state: 'success',
@@ -366,17 +366,28 @@
366366
var superPower = new SuperPower();
367367
```
368368
369-
- Use one `var` declaration for multiple variables and declare each variable on a newline.
369+
- Use one `var` declaration per variable.
370370
371371
```javascript
372372
// bad
373+
var items = getItems(),
374+
goSportsTeam = true,
375+
dragonball = 'z';
376+
377+
// good
373378
var items = getItems();
374379
var goSportsTeam = true;
375380
var dragonball = 'z';
381+
```
376382
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?
378389
var items = getItems(),
379-
goSportsTeam = true,
390+
goSportsTeam = true;
380391
dragonball = 'z';
381392
```
382393
@@ -389,17 +400,18 @@
389400
goSportsTeam = true;
390401

391402
// 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;
396408

397409
// good
398-
var items = getItems(),
399-
goSportsTeam = true,
400-
dragonball,
401-
length,
402-
i;
410+
var items = getItems();
411+
var goSportsTeam = true;
412+
var dragonball;
413+
var length;
414+
var i;
403415
```
404416
405417
- Assign variables at the top of their scope. This helps avoid issues with variable declaration and assignment hoisting related issues.
@@ -843,14 +855,18 @@
843855

844856
```javascript
845857
// bad
846-
var once
858+
var story = [
859+
once
847860
, upon
848-
, aTime;
861+
, aTime
862+
];
849863
850864
// good
851-
var once,
852-
upon,
853-
aTime;
865+
var story = [
866+
once,
867+
upon,
868+
aTime
869+
];
854870
855871
// bad
856872
var hero = {

0 commit comments

Comments
 (0)