Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
是否有与x === 0 ? true : !!x. 此表达式的目标是避免将零排除为虚假值,同时确保将其他虚假值转换为false.
x === 0 ? true : !!x
false
我能想到的最简约的语法是
x === 0 || Boolean(x)
基本上,你想允许任何数字?
typeof x === 'number'
我不确定是否有比您所拥有的更好的解决方案。你也可以!!x || x === 0使用
!!x || x === 0