Comparison Operators
These operators are used to compare two values and return a boolean result(True
or False
). They are commonly used in conditional statements to determine the flow of a program based on the comparison result.
We have the relational and equality operators in JavaScript. The relational operators are used to compare two values and return a boolean result based on the relationship between the values. The equality operators are used to compare two values for equality and return a boolean result.
Relational Operators
Relational operators are used to compare two values and return a boolean result based on the relationship between the values. Here are the relational operators in JavaScript:
1. Greater Than (>
)
The greater than operator (>
) compares two values and returns true
if the left operand is greater than the right operand, otherwise it returns false
.
let x = 5;
let y = 3;
console.log(x > y); // Output: true
2. Greater Than or Equal To (>=
)
The greater than or equal to operator (>=
) compares two values and returns true
if the left operand is greater than or equal to the right operand, otherwise it returns false
.
let x = 5;
let y = 5;
console.log(x >= y); // Output: true
3. Less Than (<
)
The less than operator (<
) compares two values and returns true
if the left operand is less than the right operand, otherwise it returns false
.
let x = 3;
let y = 5;
console.log(x < y); // Output: true
4. Less Than or Equal To (<=
)
The less than or equal to operator (<=
) compares two values and returns true
if the left operand is less than or equal to the right operand, otherwise it returns false
.
let x = 5;
let y = 5;
console.log(x <= y); // Output: true
Equality Operators
Equality operators are used to compare two values for equality and return a boolean result. Here are the equality operators in JavaScript:
1. Equal To (==
)
The equal to operator (==
) compares two values for equality and returns true
if the values are equal, otherwise it returns false
. It performs type coercion, which means it converts the operands to the same type before comparing them.
let x = 5;
let y = '5';
console.log(x == y); // Output: true
2. Not Equal To (!=
)
The not equal to operator (!=
) compares two values for inequality and returns true
if the values are not equal, otherwise it returns false
. It performs type coercion, which means it converts the operands to the same type before comparing them.
let x = 5;
let y = '5';
console.log(x != y); // Output: false
3. Strict Equal To (===
)
The strict equal to operator (===
) compares two values for equality without performing type coercion. It returns true
if the values are equal and have the same type, otherwise it returns false
.
let x = 5;
let y = '5';
console.log(x === y); // Output: false
4. Strict Not Equal To (!==
)
The strict not equal to operator (!==
) compares two values for inequality without performing type coercion. It returns true
if the values are not equal or have different types, otherwise it returns false
.
let x = 5;
let y = '5';
console.log(x !== y); // Output: true
- The equality operators (
==
,!=
) perform type coercion before comparing the values. - The strict equality operators (
===
,!==
) do not perform type coercion and compare the values without converting them to the same type.
That's it! You now know how to use comparison operators in JavaScript to compare two values and return a boolean result based on the comparison.
In the next section, we are going to be talking about Ternary operators in JavaScript. Let's keep going! 🚀
Made with ❤️ by Fasakin Henry