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
- [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.
- [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)
1035
1038
1036
-
> Why? Minimizes diff churn when adding or removing arguments.
1039
+
> Why? Minimizes diff churn when adding or removing parameters.
- [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 themassingle object.
1330
1333
1331
1334
> Why? This makes sure you have a single default export.
1332
1335
@@ -1479,7 +1482,7 @@ Other Style Guides
1479
1482
## Iterators and Generators
1480
1483
1481
1484
<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)
1483
1486
1484
1487
> Why? This enforces our immutable rule. Dealing with pure functions that return values is easier to reason about than side effects.
1485
1488
@@ -1493,18 +1496,18 @@ Other Style Guides
1493
1496
for (let num of numbers) {
1494
1497
sum += num;
1495
1498
}
1496
-
sum === 15;
1499
+
sum === 15; // true
1497
1500
1498
1501
// good
1499
1502
let sum = 0;
1500
1503
numbers.forEach((num) => {
1501
1504
sum += num;
1502
1505
});
1503
-
sum === 15;
1506
+
sum === 15; // true
1504
1507
1505
1508
// best (use the functional force)
1506
1509
const sum = numbers.reduce((total, num) => total + num, 0);
- [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
> 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.
1745
1758
1746
1759
```javascript
1747
1760
// bad
@@ -1778,7 +1791,6 @@ Other Style Guides
1778
1791
1779
1792
```javascript
1780
1793
// bad
1781
-
1782
1794
const array = [1, 2, 3];
1783
1795
let num = 1;
1784
1796
num++;
@@ -1795,7 +1807,6 @@ Other Style Guides
1795
1807
}
1796
1808
1797
1809
// good
1798
-
1799
1810
const array = [1, 2, 3];
1800
1811
let num = 1;
1801
1812
num += 1;
@@ -1835,7 +1846,6 @@ Other Style Guides
1835
1846
1836
1847
```javascript
1837
1848
// bad
1838
-
1839
1849
const some_unused_var = 42;
1840
1850
1841
1851
// Write-only variables are not considered as used.
@@ -1852,7 +1862,6 @@ Other Style Guides
1852
1862
}
1853
1863
1854
1864
// good
1855
-
1856
1865
function getXPlusY(x, y) {
1857
1866
return x + y;
1858
1867
}
@@ -2091,7 +2100,7 @@ Other Style Guides
2091
2100
let x = 1;
2092
2101
break;
2093
2102
case 2:
2094
-
const y = 2;
2103
+
const x = 2; // SyntaxError: Identifier 'x' has already been declared
2095
2104
break;
2096
2105
case 3:
2097
2106
function f() {
@@ -2109,7 +2118,7 @@ Other Style Guides
2109
2118
break;
2110
2119
}
2111
2120
case 2: {
2112
-
const y = 2;
2121
+
const x = 2; // No SyntaxError
2113
2122
break;
2114
2123
}
2115
2124
case 3: {
@@ -2156,7 +2165,7 @@ Other Style Guides
2156
2165
const foo = a ? a : b;
2157
2166
const bar = c ? true : false;
2158
2167
const baz = c ? false : true;
2159
-
const quux = a !=null ? a : b;
2168
+
const quux = (a !== undefined && a !== null) ? a : b;
2160
2169
2161
2170
// good
2162
2171
const foo = a || b;
@@ -2208,25 +2217,32 @@ Other Style Guides
2208
2217
> Why? It provides precision by distinguishing null/undefined from other falsy values, enhancing code clarity and predictability.
2209
2218
2210
2219
```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'
0 commit comments