JavaScript Comparison Operators
Comparison operators in JavaScript are essential for evaluating conditions and making decisions in your code. Let's explore both relational and equality operators along with examples:
Relational Operators ๐
Greater Than >
The greater-than operator >
checks if the left operand is greater than the right operand.
console.log(10 > 5); // true
Less Than <
The less-than operator <
checks if the left operand is less than the right operand.
console.log(10 < 5); // false
Greater Than or Equal To >=
The greater-than-or-equal-to operator >=
checks if the left operand is greater than or equal to the right operand.
console.log(10 >= 10); // true
Less Than or Equal To <=
The less-than-or-equal-to operator <=
checks if the left operand is less than or equal to the right operand.
console.log(10 <= 5); // false
Equality Operators ๐
Strict Equality ===
The strict equality operator ===
checks if both the value and the type of the left operand are equal to the right operand.
console.log(10 === '10'); // false
Strict Non-Equality !==
The strict non-equality operator !==
checks if either the value or the type of the left operand is not equal to the right operand.
console.log(10 !== '10'); // true
Loose Equality ==
The loose equality operator ==
checks if the value of the left operand is equal to the right operand, performing type coercion if needed.
console.log(10 == '10'); // true
Loose Non-Equality !=
The loose non-equality operator !=
checks if the value of the left operand is not equal to the right operand, performing type coercion if needed.
console.log(10 != '10'); // false
Conclusion ๐
Understanding and mastering these comparison operators is fundamental for building robust and expressive JavaScript code. Whether you're working with numerical values, strings, or other data types, these operators play a crucial role in implementing logical conditions in your programs. Explore and experiment with these operators to become proficient in handling various scenarios.