1

Is there a minimalistic syntax to do the same as x === 0 ? true : !!x. The goal of this expression is to avoid the exclusion of zero as falsy and yet make sure other falsy values do are converted to false.

JayCodist
  • 2,424
  • 2
  • 12
  • 28
Akheloes
  • 1,352
  • 3
  • 11
  • 28

3 Answers3

3

Basically, you want to allow any number?

typeof x === 'number'
deceze
  • 510,633
  • 85
  • 743
  • 889
  • He might want to treat NaN as falsy as well which needs an extra `&&`. – Salman A Aug 30 '21 at 12:47
  • https://stackoverflow.com/questions/68981254/operator-to-account-for-zero-but-not-undefined-null/68981425?noredirect=1#comment121915138_68981254… – deceze Aug 30 '21 at 12:48
  • 1
    FWIW, this solution is technically one character longer than OP's current solution. – TylerH Aug 30 '21 at 14:34
  • This doesn't really do it. The OP wants to test for boolean, with all numbers considered truthy. And this will evaluate to false, if x is a truthy value in another type – JayCodist Aug 31 '21 at 08:49
2

The most minimalist syntax I can think of is

x === 0 || Boolean(x)
JayCodist
  • 2,424
  • 2
  • 12
  • 28
1

I'm not sure that there's a better solution than what you've got. You could also use !!x || x === 0 though

Ashley
  • 449
  • 4
  • 13