Comparison and logical operators — JavaScript
What is this and why?
Imagine you are a robot that executes commands. To make decisions, you need to be able to compare things and check conditions. For example:
- Comparison: "Do you have 100 rubles?" (you compare your money with the number 100).
- Logic: "Do you have 100 rubles AND are you not hungry?" (you check two conditions at once).
In JavaScript, comparison operators and logical operators help the computer answer such questions. The result will always be true or false. It's like a traffic light: green — you can go (true), red — stop (false).
Why is this needed? Without them, programs would be boring — they would just execute commands in order. But with them, the program can react to different situations: for example, if the user entered the correct password — let them in, if not — show an error.
In Python, these operators work almost the same way, but JavaScript has its own quirks. We'll cover them.
Code examples with explanation
1. Basic comparison operators: >, <, >=, <=
The simplest ones — like in a math lesson. Comparing numbers:
// Example 1: Greater or less?
let age = 12;
let canEnter = age >= 12; // Can you enter if age is 12 or more?
console.log(canEnter); // Output: true (because 12 >= 12 — true)
// Example 2: Comparison with a threshold
let temperature = 30;
let isHot = temperature > 25; // Greater than 25?
console.log(isHot); // true (30 > 25 — true)
let isCold = temperature < 10; // Less than 10?
console.log(isCold); // false (30 is not less than 10)
// Example 3: Strings can also be compared!
console.log("watermelon" > "banana"); // false
// Why? Letters are compared alphabetically: 'w' (23rd letter) is greater than 'b' (2nd letter)
// In Python it's the same, but case sensitivity is different
Important: In JavaScript, strings are compared by Unicode code (like in a table). 'a' is less than 'z', and 'A' is less than 'a'. It's like a dictionary: uppercase letters come first, then lowercase.
2. Equality operators: == and ===
Here's where it gets interesting! In JavaScript, there are two ways to check if values are equal:
==— "loose equality" (compares values, but may convert types)===— "strict equality" (compares both value and data type)
// Example 4: Comparing numbers and strings
let number = 5;
let string = "5";
console.log(number == string); // true — because 5 equals 5 (JavaScript converted the string to a number)
console.log(number === string); // false — because number is not equal to string!
// In Python, this doesn't happen: "5" == 5 gives False immediately, without conversion
// Example 5: Comparison with true/false
console.log(1 == true); // true — because true is converted to 1
console.log(1 === true); // false — different types (number vs boolean)
console.log(0 == false); // true
console.log(0 === false); // false
// Example 6: Empty values
console.log(null == undefined); // true — special JS rule
console.log(null === undefined); // false — different types
// In Python, there is no undefined, only None, and it is not equal to anything except itself
Tip for beginners: Always use === (strict equality) to avoid surprises. It's like in math: 5 and "5" are different things, even if they look similar.
3. Logical operator && (AND)
The "AND" operator returns true only if both conditions are true. In life, it's like: "I will go for a walk if I do my homework AND mom allows it."
// Example 7: Checking two conditions
let age = 14;
let hasTicket = true;
let canGoToCinema = (age >= 12) && (hasTicket === true);
console.log(canGoToCinema); // true — age is suitable AND there is a ticket
// If the age were 10:
age = 10;
canGoToCinema = (age >= 12) && (hasTicket === true);
console.log(canGoToCinema); // false — the first condition is false, the second is true, but AND requires both
// Example 8: Complex condition
let grade = 4;
let behavior = "excellent";
let willBePraised = (grade === 5) && (behavior === "excellent");
console.log(willBePraised); // false — grade is not 5, although behavior is excellent
// In Python, it's written the same way: and
Life hack: JavaScript is "lazy" — if the first condition in && is false, it doesn't even check the second one. This is called "short-circuit evaluation". Saves time!
4. Logical operator || (OR)
The "OR" operator returns true if at least one condition is true. Like in life: "I will go for a walk if I do my homework OR if the sun is out."
// Example 9: Choosing from two options
let hasMoney = false;
let hasGift = true;
let canBuyToy = hasMoney || hasGift;
console.log(canBuyToy); // true — no money, but there is a gift (one condition is true)
// Example 10: Age check
let userAge = 17;
let entryAllowed = (userAge >= 18) || (userAge >= 16 && hasTicket);
// If age >=18 OR (age >=16 AND has a ticket)
console.log(entryAllowed); // false — 17 is not >=18, and although 17>=16, hasTicket is not defined (undefined — this is false)
// Example 11: Default values
let name = "";
let displayName = name || "Guest";
console.log(displayName); // "Guest" — because an empty string is considered false
// In Python: name or "Guest" — works the same way
Trick: || is also "lazy" — if the first condition is true, the second one is not checked. This is used for setting default values.