Comment by moritzwarhier

1 day ago

That's what I say in code reviews as well. Same for numbers.

!someValue is useful only for:

- booleans, including optional booleans (which is why every bool flag should default to false)

- undefined, null (falsy), or object/function (truthy)

It's nice for the second variant to also cover falsy NaN or things like this, for example for forms.

I guess that's where

  !!""===false

comes from.

But it's this exact case that keeps tripping me up.

What about empty arrays?

Per my original comment, now I'd have to look up if

  ![]

is false in PHP, or just empty([]) === true

.

So yea I agree, and extend your case to PHP "arrays" (in JS,

  !![] === true

is

  true

Obviously it depends what you're working on, what your patterns are, and so on. But my experience is that PHP involves so much array wrangling that devs are more likely to have a handle on what an array will do in this context (an empty array is falsy).

It's the string ones (in particular that in addition to the empty string, "0" specifically is the only other falsy string) that tend to catch people out.