De Morgan’s Laws are fundamental rules in mathematical logic that describe the relationships between the logical operators AND () and OR (). These laws are named after the British mathematician Augustus De Morgan.
🌐 De Morgan’s Laws
There are two main De Morgan’s Laws:
-
The negation of a conjunction is equivalent to the disjunction of the negations: This means that “not (A and B)” is equivalent to “(not A) or (not B)“.
-
The negation of a disjunction is equivalent to the conjunction of the negations: This means that “not (A or B)” is equivalent to “(not A) and (not B)“.
🚀 Examples in JavaScript
Let’s see how these laws can be applied in JavaScript.
1️⃣ Example 1: Negation of a Conjunction
De Morgan’s Law:
function example1(a, b) {
// Applying De Morgan's Law directly
const original = !(a && b);
const deMorgan = !a || !b;
console.log(`Original: ${original}, DeMorgan: ${deMorgan}`);
return original === deMorgan;
}
// Sample calls
console.log(example1(true, true)); // Output: Original: false, DeMorgan: false -> true
console.log(example1(true, false)); // Output: Original: true, DeMorgan: true -> true
console.log(example1(false, true)); // Output: Original: true, DeMorgan: true -> true
console.log(example1(false, false)); // Output: Original: true, DeMorgan: true -> true2️⃣ Example 2: Negation of a Disjunction
De Morgan’s Law:
function example2(a, b) {
// Applying De Morgan's Law directly
const original = !(a || b);
const deMorgan = !a && !b;
console.log(`Original: ${original}, DeMorgan: ${deMorgan}`);
return original === deMorgan;
}
// Sample calls
console.log(example2(true, true)); // Output: Original: false, DeMorgan: false -> true
console.log(example2(true, false)); // Output: Original: false, DeMorgan: false -> true
console.log(example2(false, true)); // Output: Original: false, DeMorgan: false -> true
console.log(example2(false, false)); // Output: Original: true, DeMorgan: true -> true🧐 Explanation
-
Negation of a Conjunction:
- In the
example1function, we check if the negation of the conjunction of two boolean values is equivalent to the disjunction of their negations. (a && b)returnstrueonly if bothaandbaretrue. The negation of this expression will betrueonly if at least one of the values isfalse.(!a || !b)istrueif at least one ofaorbisfalse.
- In the
-
Negation of a Disjunction:
- In the
example2function, we check if the negation of the disjunction of two boolean values is equivalent to the conjunction of their negations. (a || b)returnstrueif at least one ofaorbistrue. The negation of this expression will betrueonly if bothaandbarefalse.(!a && !b)is `true
- In the
true if both a and b are false.
In both cases, the functions return true if De Morgan’s Laws hold, demonstrating the equivalence of the expressions as described by the laws.
ℹ️ Conclusion
De Morgan’s Laws are powerful tools in logic and computer science, simplifying complex logical expressions and helping to understand the relationships between logical operations. By applying these laws, we can transform and simplify expressions, making our code more readable and efficient.
Understanding and using De Morgan’s Laws is essential for anyone working with logic, whether in mathematics, computer science, or everyday problem-solving.