0

I come across these kinds of expressions and I have hard time reading it. How do you read it properly to see if it is the right logic when debugging.

if (!(userLoggedIn || isAdmin)) {
    console.log('User does not have rights');
    }

closed as primarily opinion-based by durron597, user40980, user22815, GlenH7, gnat Aug 7 '15 at 5:52

Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise. If this question can be reworded to fit the rules in the help center, please edit the question.

3

Firstly expand the shorthand

if (! (userLoggedIn = true  || isAdmin = true ))

Then replace logical operators with words

if (not (userLoggedIn = true or isAdmin= true))

Then translate

If the user is not either logged in or admin
{
    //do something
}

Once you've thought about it like this the first time, the original becomes pretty trivial to read in one go - suddenly it clearly states 'if the user is not either logged in or admin' without any need to translate, this is also the beauty of good Boolean variable names as seen in this example

if (!(userLoggedIn || isAdmin))
  • 1
    Two small points: one, "either" usually means not both, but if userLoggedIn and isAdmin are true, the result will be false. Two, sometimes but not always it's useful to break it down to !userLoggedIn && !isAdmin – raptortech97 Oct 24 '14 at 2:40
  • Alternate is (in you head ) remove the !, add an empty body and insert an else. if ( (userLoggedIn || isAdmin )) {} else { do something ....} – mattnz Oct 24 '14 at 6:38
  • I find it easy to explain, with the use of or in English. 'If the user is not logged in or admin', which makes sense in English; even if mathematically it's different. – Rob Oct 24 '14 at 8:54
  • Raptor good point but I tend to assume that the reader understands the logical difference between or and xor, at least enough to understand the context of "either" - Rob's comment would avoid that, and could be a good alternative if the person has less understanding of the logical operators. In this case it seemed that the user understood the individual operators. @mattnz that's very true, but if(condition) /*nothing*/ else logic should, in my opinion, be avoided – Jon Story Oct 24 '14 at 9:00
  • I don't understand why people write "userLoggedIn == true". To me, usedLoggedIn read like "user is logged in". Adding "== true" forces you to think of userLoggedIn as a value and not a predicate. – Florian F Oct 24 '14 at 13:52
2

Generally, !(a || b) => (!a && !b),

so !(userLoggedIn || isAdmin) => !userLoggedIn && !isAdmin,

which is much clearer to me, probably due to the lack of parentheses.

0

To me the underlying logic is:

userLoggedIn || isAdmin reads "user must be logged in or in admin mode".

if(!...) reads "If it is not the case, then ...".

Taken together your code means "if it is not the case that the user is logged in or is in admin mode, then print the error message".

This makes me wonder if you didn't mean that the user must be logged in and have admin rights. In that case, you should replace || by &&.

Not the answer you're looking for? Browse other questions tagged or ask your own question.