Operator to test for falsy values with zero treated as truthy

1.2k views Asked by At

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.

3

There are 3 answers

4
deceze On BEST ANSWER

Basically, you want to allow any number?

typeof x === 'number'
0
JayCodist On

The most minimalist syntax I can think of is

x === 0 || Boolean(x)
2
Ashley On

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