Skip to content

Commit a8dab70

Browse files
committed
Update README file
1 parent c25bce8 commit a8dab70

File tree

1 file changed

+92
-60
lines changed

1 file changed

+92
-60
lines changed

README.md

Lines changed: 92 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ Other Style Guides
4646
1. [Accessors](#accessors)
4747
1. [Events](#events)
4848
1. [jQuery](#jquery)
49-
1. [ECMAScript 5 Compatibility](#ecmascript-5-compatibility)
49+
1. [ECMAScript 6 Compatibility](#ecmascript-6-compatibility)
5050
1. [ECMAScript 6+ (ES 2015+) Styles](#ecmascript-6-es-2015-styles)
5151
1. [Standard Library](#standard-library)
5252
1. [Testing](#testing)
@@ -374,9 +374,8 @@ Other Style Guides
374374
// bad
375375
const len = items.length;
376376
const itemsCopy = [];
377-
let i;
378377
379-
for (i = 0; i < len; i += 1) {
378+
for (let i = 0; i < len; i += 1) {
380379
itemsCopy[i] = items[i];
381380
}
382381
@@ -412,7 +411,7 @@ Other Style Guides
412411
```
413412

414413
<a name="arrays--mapping"></a>
415-
- [4.6](#arrays--mapping) Use [`Array.from`](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/from) instead of spread `...` for mapping over iterables, because it avoids creating an intermediate array.
414+
- [4.6](#arrays--mapping) Use [`Array.from`](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/from) instead of spread `...` for mapping over iterables like Sets, Maps or NodeLists, because it avoids creating an intermediate array.
416415

417416
```javascript
418417
// bad
@@ -710,9 +709,8 @@ Other Style Guides
710709
}
711710

712711
// good
713-
let test;
714712
if (currentUser) {
715-
test = () => {
713+
let test = () => {
716714
console.log('Yup.');
717715
};
718716
}
@@ -740,13 +738,13 @@ Other Style Guides
740738
741739
```javascript
742740
// bad
743-
function concatenateAll() {
741+
function concatenateAllStrings() {
744742
const args = Array.prototype.slice.call(arguments);
745743
return args.join('');
746744
}
747745

748746
// good
749-
function concatenateAll(...args) {
747+
function concatenateAllStrings(...args) {
750748
return args.join('');
751749
}
752750
```
@@ -854,6 +852,11 @@ Other Style Guides
854852
function f2(obj) {
855853
const key = Object.prototype.hasOwnProperty.call(obj, 'key') ? obj.key : 1;
856854
}
855+
856+
// best
857+
function f2(obj) {
858+
const key = Object.hasOwn(obj, 'key') ? obj.key : 1;
859+
}
857860
```
858861

859862
<a name="functions--reassign-params"></a><a name="7.13"></a>
@@ -1031,9 +1034,9 @@ Other Style Guides
10311034
```
10321035
10331036
<a name="arrows--one-arg-parens"></a><a name="8.4"></a>
1034-
- [8.4](#arrows--one-arg-parens) Always include parentheses around arguments for clarity and consistency. eslint: [`arrow-parens`](https://eslint.org/docs/rules/arrow-parens)
1037+
- [8.4](#arrows--one-arg-parens) Always include parentheses around parameters for clarity and consistency. eslint: [`arrow-parens`](https://eslint.org/docs/rules/arrow-parens)
10351038
1036-
> Why? Minimizes diff churn when adding or removing arguments.
1039+
> Why? Minimizes diff churn when adding or removing parameters.
10371040
10381041
```javascript
10391042
// bad
@@ -1129,6 +1132,7 @@ Other Style Guides
11291132
constructor(contents = []) {
11301133
this.queue = [...contents];
11311134
}
1135+
11321136
pop() {
11331137
const value = this.queue[0];
11341138
this.queue.splice(0, 1);
@@ -1194,8 +1198,7 @@ Other Style Guides
11941198

11951199
const luke = new Jedi();
11961200

1197-
luke.jump()
1198-
.setHeight(20);
1201+
luke.jump().setHeight(20);
11991202
```
12001203
12011204
<a name="constructors--tostring"></a><a name="9.4"></a>
@@ -1326,7 +1329,7 @@ Other Style Guides
13261329
```
13271330

13281331
<a name="modules--no-wildcard"></a><a name="10.2"></a>
1329-
- [10.2](#modules--no-wildcard) Do not use wildcard imports.
1332+
- [10.2](#modules--no-wildcard) Do not use wildcard imports unless you have multiple named exports and want to import all of them as single object.
13301333

13311334
> Why? This makes sure you have a single default export.
13321335

@@ -1479,7 +1482,7 @@ Other Style Guides
14791482
## Iterators and Generators
14801483
14811484
<a name="iterators--nope"></a><a name="11.1"></a>
1482-
- [11.1](#iterators--nope) Don’t use iterators. Prefer JavaScript’s higher-order functions instead of loops like `for-in` or `for-of`. eslint: [`no-iterator`](https://eslint.org/docs/rules/no-iterator) [`no-restricted-syntax`](https://eslint.org/docs/rules/no-restricted-syntax)
1485+
- [11.1](#iterators--nope) Don’t use iterators. Prefer JavaScript’s higher-order functions instead of loops like `for`, `for-in` or `for-of`. eslint: [`no-iterator`](https://eslint.org/docs/rules/no-iterator) [`no-restricted-syntax`](https://eslint.org/docs/rules/no-restricted-syntax)
14831486
14841487
> Why? This enforces our immutable rule. Dealing with pure functions that return values is easier to reason about than side effects.
14851488
@@ -1493,18 +1496,18 @@ Other Style Guides
14931496
for (let num of numbers) {
14941497
sum += num;
14951498
}
1496-
sum === 15;
1499+
sum === 15; // true
14971500
14981501
// good
14991502
let sum = 0;
15001503
numbers.forEach((num) => {
15011504
sum += num;
15021505
});
1503-
sum === 15;
1506+
sum === 15; // true
15041507
15051508
// best (use the functional force)
15061509
const sum = numbers.reduce((total, num) => total + num, 0);
1507-
sum === 15;
1510+
sum === 15; // true
15081511
15091512
// bad
15101513
const increasedByOne = [];
@@ -1640,12 +1643,22 @@ Other Style Guides
16401643
## Variables
16411644
16421645
<a name="variables--const"></a><a name="13.1"></a>
1643-
- [13.1](#variables--const) Always use `const` or `let` to declare variables. Not doing so will result in global variables. We want to avoid polluting the global namespace. Captain Planet warned us of that. eslint: [`no-undef`](https://eslint.org/docs/rules/no-undef) [`prefer-const`](https://eslint.org/docs/rules/prefer-const)
1646+
- [13.1](#variables--const) Always use `const` or `let` to declare variables. Not doing so will result in global variables in case strict mode is disabled. We want to avoid polluting the global namespace. Captain Planet warned us of that. eslint: [`no-undef`](https://eslint.org/docs/rules/no-undef) [`prefer-const`](https://eslint.org/docs/rules/prefer-const)
1647+
1648+
> Note: The entire contents of JavaScript modules are automatically in [strict mode](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode) and therefore usage of the undeclared variables will not lead to creation of the global variables
16441649
16451650
```javascript
16461651
// bad
16471652
superPower = new SuperPower();
16481653
1654+
// bad
1655+
function foo() {
1656+
bar = 10 // bar will appear in the global scope
1657+
}
1658+
1659+
foo()
1660+
console.log(bar) // 10
1661+
16491662
// good
16501663
const superPower = new SuperPower();
16511664
```
@@ -1741,7 +1754,7 @@ Other Style Guides
17411754
<a name="variables--no-chain-assignment"></a><a name="13.5"></a>
17421755
- [13.5](#variables--no-chain-assignment) Don’t chain variable assignments. eslint: [`no-multi-assign`](https://eslint.org/docs/rules/no-multi-assign)
17431756
1744-
> Why? Chaining variable assignments creates implicit global variables.
1757+
> Why? Chaining variable assignments creates implicit global variables in case [strict mode](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode) is disabled.
17451758
17461759
```javascript
17471760
// bad
@@ -1778,7 +1791,6 @@ Other Style Guides
17781791
17791792
```javascript
17801793
// bad
1781-
17821794
const array = [1, 2, 3];
17831795
let num = 1;
17841796
num++;
@@ -1795,7 +1807,6 @@ Other Style Guides
17951807
}
17961808
17971809
// good
1798-
17991810
const array = [1, 2, 3];
18001811
let num = 1;
18011812
num += 1;
@@ -1835,7 +1846,6 @@ Other Style Guides
18351846
18361847
```javascript
18371848
// bad
1838-
18391849
const some_unused_var = 42;
18401850
18411851
// Write-only variables are not considered as used.
@@ -1852,7 +1862,6 @@ Other Style Guides
18521862
}
18531863
18541864
// good
1855-
18561865
function getXPlusY(x, y) {
18571866
return x + y;
18581867
}
@@ -2091,7 +2100,7 @@ Other Style Guides
20912100
let x = 1;
20922101
break;
20932102
case 2:
2094-
const y = 2;
2103+
const x = 2; // SyntaxError: Identifier 'x' has already been declared
20952104
break;
20962105
case 3:
20972106
function f() {
@@ -2109,7 +2118,7 @@ Other Style Guides
21092118
break;
21102119
}
21112120
case 2: {
2112-
const y = 2;
2121+
const x = 2; // No SyntaxError
21132122
break;
21142123
}
21152124
case 3: {
@@ -2156,7 +2165,7 @@ Other Style Guides
21562165
const foo = a ? a : b;
21572166
const bar = c ? true : false;
21582167
const baz = c ? false : true;
2159-
const quux = a != null ? a : b;
2168+
const quux = (a !== undefined && a !== null) ? a : b;
21602169
21612170
// good
21622171
const foo = a || b;
@@ -2208,25 +2217,32 @@ Other Style Guides
22082217
> Why? It provides precision by distinguishing null/undefined from other falsy values, enhancing code clarity and predictability.
22092218

22102219
```javascript
2211-
// bad
2212-
const value = 0 ?? 'default';
2213-
// returns 0, not 'default'
2214-
2215-
// bad
2216-
const value = '' ?? 'default';
2217-
// returns '', not 'default'
2220+
// good
2221+
const value = '' ?? 'default'; // '', not 'default'
22182222
22192223
// good
2220-
const value = null ?? 'default';
2221-
// returns 'default'
2224+
const value = null ?? 'default'; // 'default'
22222225
22232226
// good
22242227
const user = {
22252228
name: 'John',
22262229
age: null
22272230
};
2228-
const age = user.age ?? 18;
2229-
// returns 18
2231+
const age = user.age ?? 18; // 18
2232+
2233+
// good
2234+
const user = {
2235+
name: 'John',
2236+
};
2237+
const age = user.age ?? 18; // 18
2238+
2239+
// good
2240+
const user = {
2241+
name: 'John',
2242+
age: 0
2243+
};
2244+
const age = user.age ?? 18; // 0
2245+
const defaultAge = user.age || 18 // 18
22302246
```
22312247

22322248
**[⬆ back to top](#table-of-contents)**
@@ -2455,11 +2471,11 @@ Other Style Guides
24552471
24562472
```javascript
24572473
// bad
2458-
const active = true; // is current tab
2474+
const isActive = true; // is current tab
24592475
24602476
// good
24612477
// is current tab
2462-
const active = true;
2478+
const isActive = true;
24632479
24642480
// bad
24652481
function getType() {
@@ -2688,10 +2704,10 @@ Other Style Guides
26882704
// good
26892705
$('#items')
26902706
.find('.selected')
2691-
.highlight()
2692-
.end()
2707+
.highlight()
2708+
.end()
26932709
.find('.open')
2694-
.updateCount();
2710+
.updateCount();
26952711
26962712
// bad
26972713
const leds = stage.selectAll('.led').data(data).enter().append('svg:svg').classed('led', true)
@@ -2700,14 +2716,16 @@ Other Style Guides
27002716
.call(tron.led);
27012717
27022718
// good
2703-
const leds = stage.selectAll('.led')
2704-
.data(data)
2705-
.enter().append('svg:svg')
2706-
.classed('led', true)
2707-
.attr('width', (radius + margin) * 2)
2719+
const leds = stage
2720+
.selectAll('.led')
2721+
.data(data)
2722+
.enter()
2723+
.append('svg:svg')
2724+
.classed('led', true)
2725+
.attr('width', (radius + margin) * 2)
27082726
.append('svg:g')
2709-
.attr('transform', `translate(${radius + margin}, ${radius + margin})`)
2710-
.call(tron.led);
2727+
.attr('transform', `translate(${radius + margin}, ${radius + margin})`)
2728+
.call(tron.led);
27112729
27122730
// good
27132731
const leds = stage.selectAll('.led').data(data);
@@ -2948,11 +2966,12 @@ Other Style Guides
29482966
?.xyzzy;
29492967

29502968
// good
2951-
$.ajax({
2952-
method: 'POST',
2953-
url: 'https://airbnb.com/',
2954-
data: { name: 'John' },
2955-
})
2969+
$
2970+
.ajax({
2971+
method: 'POST',
2972+
url: 'https://airbnb.com/',
2973+
data: { name: 'John' },
2974+
})
29562975
.done(() => console.log('Congratulations!'))
29572976
.fail(() => console.log('You have failed this city.'));
29582977
```
@@ -3190,6 +3209,20 @@ Other Style Guides
31903209
inventorOf,
31913210
...heroArgs
31923211
);
3212+
3213+
// bad
3214+
import {
3215+
firstVariable,
3216+
secondVariable,
3217+
thirdVariable
3218+
} from './someModule';
3219+
3220+
// good
3221+
import {
3222+
firstVariable,
3223+
secondVariable,
3224+
thirdVariable,
3225+
} from './someModule';
31933226
```
31943227
31953228
**[⬆ back to top](#table-of-contents)**
@@ -3347,7 +3380,7 @@ Other Style Guides
33473380
}
33483381

33493382
// good
3350-
function query() {
3383+
function makeQuery() {
33513384
// ...
33523385
}
33533386
```
@@ -3743,10 +3776,10 @@ Other Style Guides
37433776

37443777
**[⬆ back to top](#table-of-contents)**
37453778

3746-
## ECMAScript 5 Compatibility
3779+
## ECMAScript 6 Compatibility
37473780

3748-
<a name="es5-compat--kangax"></a><a name="26.1"></a>
3749-
- [27.1](#es5-compat--kangax) Refer to [Kangax](https://twitter.com/kangax/)’s ES5 [compatibility table](https://kangax.github.io/es5-compat-table/).
3781+
<a name="ecmascript-6-compatibility"></a><a name="26.1"></a>
3782+
- [27.1](#ecmascript-6-compatibility) Refer to [Can I use](https://caniuse.com/es6) for features compatibility
37503783

37513784
**[⬆ back to top](#table-of-contents)**
37523785

@@ -3860,8 +3893,7 @@ Other Style Guides
38603893

38613894
- [Latest ECMA spec](https://tc39.github.io/ecma262/)
38623895
- [ExploringJS](https://exploringjs.com/)
3863-
- [ES6 Compatibility Table](https://compat-table.github.io/compat-table/es6/)
3864-
- [Comprehensive Overview of ES6 Features](https://web.archive.org/web/20240404212626/http://es6-features.org/)
3896+
- [Comprehensive Overview of ES6 Features](http://es6-features.org/)
38653897
- [JavaScript Roadmap](https://roadmap.sh/javascript)
38663898

38673899
**Read This**

0 commit comments

Comments
 (0)