views:

61

answers:

3

Sorry if this is a "Logical Operators 101" kind of question. I've been staring at my screen for 15 minutes trying to wrap my head around it, but I'm stuck.

Is there a more concise/elegant way to phrase the following (this is JavaScript syntax, but it's not a language-dependent question):

if (!something && !something_else) {
  // do something
}

Based on some experimentation, this does not appear to be the logical equivalent:

if (!(something && something_else) {
  // do something
}

In addition, can anyone recommend an online resource(s) for further study on questions like these? I'm assuming that this type of thing is covered on an abstract level in computer science curricula, and it's an essential gap in my programming knowledge that I'd really like to fill. Thanks!

+9  A: 
(Not A) And (Not B)

is equivalent to:

Not (A Or B)

It's an application of De Morgan's laws

Mitch Wheat
+1 Break the line, change the sign!
Preet Sangha
Thanks, Mitch! All correct answers here - and all are much appreciated - but I also appreciate the link you included. That's exactly what I was looking for.
Bungle
@Bungle. You should accept his answer then. He'll appreciate that I'm sure
Ben
@Ben: Will do. Looks like SO makes you wait ~10 minutes to do so, though.
Bungle
+3  A: 

The proper expression should be:

if (!(something || something else)) {
  // do something
}

When you apply a negation it switches the operators between AND and OR.

Nathan S.
+3  A: 

I think, you need

if (!(something || something_else)) {
  // do something
}

!something && !something_else means "neither something and neither something_else", which is equivalent to "neither (something or something_else)"

Ben