clean-code-javascript
clean-code-javascript copied to clipboard
Use default arguments instead of short circuiting or conditionals
It's mentioned that Other "falsy" values than undefined such as '', "", false, null, 0, and NaN, will not be replaced by a default value. I am doubtful as below line of code works with default value; let a = ""; let b = a || "my default value"; console.log(b) // prints my default value This even works for other falsy values.
I think that's the point. Consider this example:
function setEnable(enable) {
enable = enable || true
console.log(enable);
}
setEnable(); // true
setEnable(true); // true
setEnable(false); // true
Setting the param as false is not the same as leaving it undefined
https://mrale.ph/blog/2018/02/03/maybe-you-dont-need-rust-to-speed-up-your-js.html#optimizing-sorting---argument-adaptation