In this chapter, we will learn about JavaScript comparison operators. These operators are used to compare two values and return a boolean value (true
or false
). We will cover:
- Equal to (==)
- Strict Equal to (===)
- Not Equal to (!=)
- Strict Not Equal to (!==)
- Greater than (>)
- Greater than or Equal to (>=)
- Less than (<)
- Less than or Equal to (<=)
Equal to (==)
The equal to operator compares two values for equality, converting their types if necessary.
Syntax
value1 == value2;
Example
let result = (10 == "10"); console.log(result); // Output: true
Strict Equal to (===)
The strict equal to operator compares two values for equality without converting their types.
Syntax
value1 === value2;
Example
let result = (10 === "10"); console.log(result); // Output: false
Not Equal to (!=)
The not equal to operator compares two values for inequality, converting their types if necessary.
Syntax
value1 != value2;
Example
let result = (10 != "10"); console.log(result); // Output: false
Strict Not Equal to (!==)
The strict not equal to operator compares two values for inequality without converting their types.
Syntax
value1 !== value2;
Example
let result = (10 !== "10"); console.log(result); // Output: true
Greater than (>)
The greater than operator checks if the left value is greater than the right value.
Syntax
value1 > value2;
Example
let result = (10 > 5); console.log(result); // Output: true
Greater than or Equal to (>=)
The greater than or equal to operator checks if the left value is greater than or equal to the right value.
Syntax
value1 >= value2;
Example
let result = (10 >= 10); console.log(result); // Output: true
Less than (<)
The less than operator checks if the left value is less than the right value.
Syntax
value1 < value2;
Example
let result = (10 < 5); console.log(result); // Output: false
Less than or Equal to (<=)
The less than or equal to operator checks if the left value is less than or equal to the right value.
Syntax
value1 <= value2;
Example
let result = (10 <= 5); console.log(result); // Output: false
Conclusion
In this chapter, you learned about JavaScript comparison operators, including equal to, strict equal to, not equal to, strict not equal to, greater than, greater than or equal to, less than, and less than or equal to. These operators are essential for making decisions in your code by comparing values and determining their relationships. In the next chapter, we will explore JavaScript logical operators and how to use them in your programs.