Use default arguments instead of short circuiting or conditionals
Default arguments are often cleaner than short circuiting. Be aware that if you
use them, your function will only provide default values for undefined
arguments. Other “falsy” values such as ''
, ""
, false
, null
, 0
, andNaN
, will not be replaced by a default value.
Bad:1234function createMicrobrewery(name) { const breweryName = name || 'Hipster Brew Co.'; // ...}
Good:123function createMicrobrewery(name = 'Hipster Brew Co.') { // ...}