Javascript Comparison Operators
In this lesson, we will learn about Comparison operators, which are operators for comparisons.
They are used with condition and decision making, and return true
if the condition is met or false
if the condition is not met.
Equal to (==)
This operator compares two values to see if they are the same
- If the values are equal it will return
true
. - If the values are not equal it will return
false
.
Example:
console.log(700 == '700'); // Output: true
In this example we compare two values, the first value is 700 and the second value is '700', and it returns true
because the 700 equals 700.
Example:
console.log('John' == 'John'); // Output: true
In this example, we compare two values. The first value is 'John', and the second value is also 'John', and it returns true
because 'John' is equal to 'John'.
Example:
console.log(700 == 400); // Output: false
In this example, we compare two values. The first value is 700, and the second value is 400.
it returns false
because 700 is not equal to 400.
Not Equal to (!=)
This operator compares two values to see if they are not the same.
- If the values are not equal it will return
true
. - If the values are equal it will return
false
.
Example:
console.log(700 != 600); // Output: true
In this example, we compare two values: the first value is 700, and the second value is 600, using the "Not Equal to" operator (!=)
.
Returns true
because 700 is not equal to 600. true because 700 is not equal to 600
Example:
console.log('John' != 'John'); // Output: false
In this example we compare two values, the first value is John and the second value is John using Not Equal to operator (!=)
.
Returns false
because John is equal to John
Strict Equal to (===)
This operator Compares two values to see if they are the same in value and type.
- Returns
true
if the two values are the same in value and type. - Returns
false
if the two values are not the same in value and type.
Example:
console.log(700 === 700); // Output: true
In this example we compare between the two values,700 and 700 in terms of value, and type of the value, using the strict equal to operator (===)
and it returns true
.
Because, it compared both values. The operator finds out that the first value is 700, and the second is 700, which is the same value. And when it compared the type of the value too, it found out that both types are numbers, which returned true
as well.
Now if we look at this example:
console.log(700 === 200); // Output: false
In this example, we compare between the two values, 700 and 200 using the strict equal to (===)
and it returns false
. Because while both values have the same type, they don’t have the same value.
Strict Not Equal to (!==)
Compares between two values based on both value and type to determine if they’re not the same. It is the opposite of the strict equal to operator.
It returns true
if both values are not the same, and false
if they’re the same.
Look at the following example:
console.log(10 !== 10); // Output: false
In this example, it returned false
, as both the values are the same value and type.
Look at the following example:
console.log('John' !== 'Jeff'); // Output: true
In this example instead, it returns true
as both values are not the same (same type, different value)
Look at the following example:
console.log(10 !== '10'); // Output: true
In this one, it returned true
as both values are not the same (same value, different type)
Greater than (>)
Check if the left number is greater than the right number.
- Returns
true
if the left number is greater than the right number. - Returns
false
if the right number is greater than the left number.
Example:
console.log(5 > 2); // Output: true
In this example the result is true
because 5 (the left number) is greater than 2 (the right number).
Example:
console.log(5 > 10); // Output: false
In this example the result is false
because 5 (the left number) is less than 10 (the right number).
Greater than or equal to (>=)
Checks if the left number is greater than or equal to the right number.
- Returns
true
if the left number is greater than or equal to the right number. - Returns
false
if the right number is greater than the left number.
Example:
console.log(120 >= 100); // Output: true
In this example the result is true
because 120 (the left number) is greater than 100 (the right number).
Example:
console.log(100 >= 100); // Output: true
In this example the result is true
because 100 (the left number) equals 100 (the right number).
Example:
console.log(95 >= 100); // Output: false
In this example the result is false
because 95 (the left number) is less than 100 (the right number).
less than (<)
Check if the left number is smaller than the right number.
- Returns
true
if the left number is smaller than the right number. - Returns
false
if the left number is greater than the right number.
Example:
console.log(20 < 30); // Output: true
In this example, the result is true
because 20 (the left number) is smaller than 30 (the right number).
Example:
console.log(220 < 100); // Output: false
In this example the result is false
because 220 (the left number) is greater than 100 (the right number).
less than or equal to (<=)
Checks if the left number is smaller than or equal to the right number.
- Returns
true
if the left number is less than or equal to the right number. - Returns
false
if the left number is greater than the right number.
Example:
console.log(5 <= 10); // Output: true
In this example the result is true
because 5 (the left number) is smaller than 10 (the right number).
Example:
console.log(10 <= 10); // Output: true
In this example the result is true
because 10 (the left number) equals 10 (the right number).
Example:
console.log(20 <= 10); // Output: false
In this example the result is false
because 20 (the left number) is greater than 10 (the right number).