DEV Community

Cover image for Clear as the sun explanation of De Morgan's laws
Marat Latypov
Marat Latypov

Posted on • Updated on

Clear as the sun explanation of De Morgan's laws

Sometimes, novice developers struggle with basic logic expressions, especially De Morgan's laws. To help with this, I have prepared a simple explanation.

The following examples were made using a small React app that I created for this article. You can choose to download and play with it or not 😀️. The following text doesn't require the app installation.

Step 1. Where are we from

Ok. Let start with the basics. How looks a&&b result? I think everybody knows:

a && b

Step 2. Let's play with it

What if we negate b in this expression? Just imagine the column where b was true will changed to false, and vice versa. The matrix will be flipped horizontally, like reflection in a mirror!
a && b => a && !b

And if we negate a, it will be flipped vertically:
a && b => !a && b

Step 3. Try a little bit harder

What if we negate both a and b the same time? Right! The matrix will be flipped in both directions - horizontally and vertically:

a && b => !a && !b

Step 4. The Final

Nice, and what if we negate the entire expression after negating a and b? In that case the values in the matrix will change from true to false, and vice versa:

!a && !b => !(!a && !b)

But wait! It looks like matrix for a||b!

de Morgan's Law

Actually that's the de Morgan's Law

!(!a&&!b) === a||b or !a&&!b === !(a||b)

I hope this article was helpful.

Top comments (0)