bevacqua / js

:art: A JavaScript Quality Guide
https://ponyfoo.com
MIT License
2.92k stars 497 forks source link

Default value #16

Closed Hurtak closed 9 years ago

Hurtak commented 9 years ago

I don't think it's good to recommend using || for setting default value to variables. It's nice shortcut but it can cause trouble when valid input (0, false, '') is converted to default value. I would recommend mentioning explicitly these dangers in that chapter or add chapter about more safe default value assigning

function example(value) {
  if (typeof value === 'undefined') {
    value = 'defaultValue';
  }
}
bevacqua commented 9 years ago

You can always add the recommendation in a pull request. I'd advice against the typeof check, I prefer comparing against void 0 for brevity.

Hurtak commented 9 years ago

Why would you advice against typeof check?

bevacqua commented 9 years ago

brevity