&& and || in the same expression in Javascript

In some cases I've seen expressions like:

!form.$valid && 'invalid' || 'valid'

      

Going on the left-to-right priority given here , it seems to mean

(!form.$valid && 'invalid') || 'valid'

      

I am confused as to how this returns a string. I think these strings true

are because they are not null

or undefined

, which means the expression always returns true

.

+3


source to share


1 answer


In your example, you are not comparing Boolean VS Boolean, but:

(Boolean VS String) VS String

You can see a comparison with one String

(right or left), returning String

only if the other operand true

.



This is why the return is either "invalid" or "valid".

(The rules are the same for another non-boolean value.)

0


source







All Articles