JavaScript Comparison Operators
- Published
- Reading time
- 2 min read
- Length
- 391 words
On this page
JavaScript Comparison Operators#
JavaScript comparison operators are used to compare values and return a boolean value (true or false) based on the comparison result. There are various types of comparison operators in JavaScript, such as:
Equal operator (==)#
The equal operator compares two values for equality. It returns true if the operands are equal, or false otherwise.
var x = 10;
var y = 5;
var result = x == y;
// result is false
Strict equal operator (===)#
The strict equal operator compares two values for equality without type coercion. It returns true if the operands are equal and of the same type, or false otherwise.
var x = 10;
var y = "10";
var result = x === y;
// result is false
Not equal operator (!=)#
The not equal operator compares two values for inequality. It returns true if the operands are not equal, or false otherwise.
var x = 10;
var y = 5;
var result = x != y;
// result is true
Strict not equal operator (!==)#
The strict not equal operator compares two values for inequality without type coercion. It returns true if the operands are not equal or of different types, or false otherwise.
var x = 10;
var y = "10";
var result = x !== y;
// result is true
Greater than operator (>)#
The greater than operator compares two values and returns true if the left operand is greater than the right operand, or false otherwise.
var x = 10;
var y = 5;
var result = x > y;
// result is true
Less than operator (<)#
The less than operator compares two values and returns true if the left operand is less than the right operand, or false otherwise.
var x = 10;
var y = 5;
var result = x < y;
// result is false
Greater than or equal to operator (>=)#
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, or false otherwise.
var x = 10;
var y = 5;
var result = x >= y;
// result is true
Less than or equal to operator (<=)#
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, or false otherwise.
var x = 10;
var y = 5;
var result = x <= y;
// result is false
These are the basic comparison operators used in JavaScript. You can use them to compare any two values in your code.