๐ค What are Logical Operators? Logical operators are special symbols that help us make decisions in our code. They work like traffic lights - they tell us when to go, stop, or check something. Just like in real life, we use words like 'and', 'or', and 'not' to make choices! ๐ฆ
๐ Introduction In JavaScript, logical operators help us combine different conditions and make smart decisions. Think of them as helpers that check if things are true or false. They are very useful when we want our program to do different things based on different situations. Let's learn them step by step! ๐ฏ
๐ All Logical Operators in JavaScript
There are three main logical operators in JavaScript:
โข AND (&&) - Both conditions must be true โ โ
โข OR (||) - At least one condition must be true โ or โ
โข NOT (!) - Makes true become false, and false become true ๐
๐ก The AND Operator (&&)
The AND operator is like saying 'both things must be true'. Think of it like this: You can go to the park AND play football only if it's sunny AND you have finished your homework. Both conditions must be true! โ๏ธ๐
1
2
3
4
5
6
7
8
9
10
11
12
13
// AND Operator Example
let age = 20;
let hasLicense = true;
// Both conditions must be true
if (age >= 18 && hasLicense) {
console.log("You can drive a car! ๐");
} else {
console.log("You cannot drive yet ๐");
}
// Result: "You can drive a car! ๐"
// Because age is 20 (>=18 is true) AND hasLicense is true
๐ The OR Operator (||)
The OR operator is like saying 'at least one thing must be true'. Think of it like this: You can have ice cream if it's your birthday OR if you got good grades. You only need one of these to be true! ๐ฆ๐
1
2
3
4
5
6
7
8
9
10
11
12
13
// OR Operator Example
let isBirthday = false;
let gotGoodGrades = true;
// At least one condition must be true
if (isBirthday || gotGoodGrades) {
console.log("You can have ice cream! ๐ฆ");
} else {
console.log("No ice cream today ๐");
}
// Result: "You can have ice cream! ๐ฆ"
// Because gotGoodGrades is true (even though isBirthday is false)
๐ The NOT Operator (!)
The NOT operator is like saying 'the opposite'. It changes true to false and false to true. Think of it like a magic switch that flips everything! ๐ช
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// NOT Operator Example
let isRaining = true;
let isNotRaining = !isRaining;
console.log("Is it raining?", isRaining); // true
console.log("Is it NOT raining?", isNotRaining); // false
// Another example
let isWeekend = false;
if (!isWeekend) {
console.log("It's a school day! ๐");
} else {
console.log("It's weekend! ๐");
}
// Result: "It's a school day! ๐"
// Because !isWeekend means "not weekend" which is true
๐ฏ Practice Examples
Let's practice with some real-world examples that you might use in your programs:
Example 1: Age and Marriage Check
1
2
3
4
5
6
7
8
9
10
11
12
13
// Alert only when user is over 18 AND is married
let userAge = 25;
let isMarried = true;
if (userAge > 18 && isMarried) {
alert("Welcome! You meet both requirements ๐");
} else if (userAge <= 18) {
alert("You must be over 18 years old ๐
");
} else if (!isMarried) {
alert("You must be married to continue ๐");
}
// This will show: "Welcome! You meet both requirements ๐"
Example 2: Website Access Check
1
2
3
4
5
6
7
8
9
10
11
12
// Allow access if user is admin OR has premium account
let isAdmin = false;
let hasPremium = true;
if (isAdmin || hasPremium) {
console.log("Access granted! Welcome to premium features ๐");
} else {
console.log("Please upgrade to premium or contact admin ๐");
}
// Result: "Access granted! Welcome to premium features ๐"
// Because hasPremium is true (even though isAdmin is false)
Example 3: Game Permission Check
1
2
3
4
5
6
7
8
9
10
11
12
13
14
// Check if someone can play a game
let age = 16;
let hasParentPermission = false;
let isWeekend = true;
// Can play if: (age >= 18) OR (age >= 13 AND has parent permission AND it's weekend)
if (age >= 18 || (age >= 13 && hasParentPermission && isWeekend)) {
console.log("You can play the game! ๐ฎ");
} else {
console.log("You cannot play right now ๐");
}
// Result: "You cannot play right now ๐"
// Because age is 16 (not >= 18) AND hasParentPermission is false
Example 4: Shopping Cart Check
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// Check if user can complete purchase
let hasItems = true;
let hasPaymentMethod = true;
let isLoggedIn = false;
// All conditions must be true for purchase
if (hasItems && hasPaymentMethod && isLoggedIn) {
console.log("Purchase completed! ๐โ
");
} else {
if (!hasItems) {
console.log("Your cart is empty! Add some items first ๐๏ธ");
} else if (!hasPaymentMethod) {
console.log("Please add a payment method ๐ณ");
} else if (!isLoggedIn) {
console.log("Please login to complete purchase ๐");
}
}
// Result: "Please login to complete purchase ๐"
// Because isLoggedIn is false (even though other conditions are true)
๐ Summary
Logical operators are like decision-makers in your code:
โข AND (&&) - All conditions must be true (like having both keys AND password)
โข OR (||) - At least one condition must be true (like having cash OR credit card)
โข NOT (!) - Flips true to false and false to true (like turning a light switch)
Practice with these examples and you'll become a master of logical operators! Remember, they help make your programs smart and responsive to different situations. Happy coding! ๐๐จโ๐ป