Recent Post:
Categories:
September 22, 2024
152
Think you know it all?
Take quiz!Operators are like tools that help you compare values or make decisions in your code. In JavaScript, there are logical operators (which work with true/false values) and comparison operators (which help you compare numbers or strings).
code
let isSunny = true;
let isWarm = false;
console.log(isSunny && isWarm); // Output: false
Explanation:
In this case, isSunny is true, but isWarm is false. Since both need to be true for && to return true, the result is false.
code
console.log(isSunny || isWarm); // Output: true
Here, since isSunny is true, the result is true even though isWarm is false. As long as one condition is true, || returns true.
1. Less Than (<): Checks if the left side is smaller than the right side.
code
console.log(5 < 10); // Output: true
5 is less than 10, so this returns true.
2. Greater Than (>): Checks if the left side is greater than the right side.
code
console.log(15 > 10); // Output: true
15 is greater than 10, so this returns true.
3. Less Than or Equal To (<=): Checks if the left side is less than or equal to the right side.
code
console.log(10 <= 10); // Output: true
Since 10 is equal to 10, this returns true.
4. Greater Than or Equal To (>=): Checks if the left side is greater than or equal to the right side.
code
console.log(20 >= 25); // Output: false
20 is not greater than or equal to 25, so this returns false.
1. Equal (==): Checks if two values are equal, but it doesn’t consider the type.
code
console.log(5 == '5'); // Output: true
Here, 5 (a number) is equal to '5' (a string) because == converts the string to a number before comparing.
2. Strict Equal (===): Checks if two values are equal and also checks if they are of the same type.
code
console.log(5 === '5'); // Output: false
This returns false because one is a number and the other is a string, so they are not strictly equal.
1. Not Equal (!=): Checks if two values are not equal, without considering the type.
code
console.log(5 != '5'); // Output: false
This returns false because 5 is considered equal to '5'.
2. Strict Not Equal (!==): Checks if two values are not equal and also checks if they are of different types.
code
console.log(5 !== '5'); // Output: true
This returns true because one is a number and the other is a string.