Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6794252

Browse files
Merge pull request ryanmcdermott#34 from acparas/master
Rephrase default value advice
2 parents f93513e + eaa3477 commit 6794252

File tree

1 file changed

+7
-28
lines changed

1 file changed

+7
-28
lines changed

README.md

Lines changed: 7 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -174,25 +174,23 @@ function paintCar(car) {
174174
```
175175
**[⬆ back to top](#table-of-contents)**
176176

177-
### Short-circuiting is cleaner than conditionals
177+
### Use default arguments instead of short circuiting or conditionals
178178

179179
**Bad:**
180180
```javascript
181181
function createMicrobrewery(name) {
182-
let breweryName;
183-
if (name) {
184-
breweryName = name;
185-
} else {
186-
breweryName = 'Hipster Brew Co.';
187-
}
182+
const breweryName = name || 'Hipster Brew Co.';
183+
...
188184
}
185+
189186
```
190187

191188
**Good**:
192189
```javascript
193-
function createMicrobrewery(name) {
194-
const breweryName = name || 'Hipster Brew Co.'
190+
function createMicrobrewery(breweryName = 'Hipster Brew Co.') {
191+
...
195192
}
193+
196194
```
197195
**[⬆ back to top](#table-of-contents)**
198196

@@ -431,25 +429,6 @@ function showList(employees) {
431429
```
432430
**[⬆ back to top](#table-of-contents)**
433431

434-
### Use default arguments instead of short circuiting
435-
**Bad:**
436-
```javascript
437-
function writeForumComment(subject, body) {
438-
subject = subject || 'No Subject';
439-
body = body || 'No text';
440-
}
441-
442-
```
443-
444-
**Good**:
445-
```javascript
446-
function writeForumComment(subject = 'No subject', body = 'No text') {
447-
// ...
448-
}
449-
450-
```
451-
**[⬆ back to top](#table-of-contents)**
452-
453432
### Set default objects with Object.assign
454433

455434
**Bad:**

0 commit comments

Comments
 (0)