bevacqua / js

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

Default Values #4

Closed dannyfritz closed 10 years ago

dannyfritz commented 10 years ago

I've always had a problem with the default values trick using ||. What if you want to default the used value to 0?

https://github.com/bevacqua/js/tree/3aa78500a96e4ca5cf13867593081da07b83f31c#everyday-tricks

bevacqua commented 10 years ago

Use used || 0, then

bevacqua commented 10 years ago

If you want to allow falsy values then you need a ternary expression

value === void 0 ? defaultValue : value