clean-code-javascript icon indicating copy to clipboard operation
clean-code-javascript copied to clipboard

Use default arguments instead of short circuiting or conditionals

Open mdv27 opened this issue 8 years ago • 2 comments

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.

mdv27 avatar Sep 27 '17 03:09 mdv27

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

MattShaile avatar Apr 08 '18 09:04 MattShaile

https://mrale.ph/blog/2018/02/03/maybe-you-dont-need-rust-to-speed-up-your-js.html#optimizing-sorting---argument-adaptation

kurtextrem avatar Apr 08 '18 10:04 kurtextrem