JS Operators Part Two
3. Comparison Operator
Comparison operators are used to compare two values in a Boolean pattern
Operator Name
== Equal
=== Identical // check equality of data type
!= Not equal
!== Not identical //check inequality of data type
< Less than
> Greater than
>= Greater than or equal to
<= Less than or equal to
Result of Comparison
Comparison returns a logical value (Boolean)
True (or) False


Check for the corresponding line number in the console for readability.
Comparison of different types
Let's see whats happens if we compare a number type with a number as a string data type (in “ ”)

Here second operand (argument) in all expressions are String type. Check corresponding line numbers in the console for readability.
The second operand 5 and 10 are of String data type, but the results we are getting are correct.
When the values are of different Data Types, while comparing, they are converted to Number Data Type. Here, String “5” and “10” are converted to Number 5 and 10
Boolean and Number Comparison
In the case of Boolean and Numbers,
True becomes 1 and False becomes 0

line no.11 is an empty string used for readability.

Line 11 : console.log(“Raise” > “Race”);Javascript Engine compares first two letters : R and R are same
Javascript Engine compares second two letters : a and a are same
Javascript Engine compares third two letters : i and c are not samei is greater than c. Engine stops comparing and gives the result First String is greater.
Unicode order
Case matters in String comparison

Because Javascript uses Unicode order to compare Strings, where “a” has a greater index than “A” in the Unicode table.
Strict Equality
“== “and “===”
1. ==
The normal equality operator “==” converts different Data Types to a number

As the == operator converts everything to a number, So there is no difference in true and 1, false and 0, but what if we need to check the difference between 1 and true or 1 and “1”,
or the question could be
How can we check the difference between 1 and “1” .?
2. ===
“===” checks the equality of Data Type.
Equality check without Type conversion
console.log(1=== “1”); // false, because 1 is number and “1” is String.

Find the difference of == and === from lines 4 and 5 with their results in the console.
line 4 : Equality : String "1" converted to Number. So true
line 5 : Strict Equality : Checks the equality of DataType. 1 is number and "1" is string. So false
line 6 : Empty string
line 8 : Boolean "true" is converted to number 1. So true
line 9 : Strict equality : Boolean true is taken as Boolean Type. So false
.....So on....
Strict non-equality
!==, Does the non-equality check for the Data Type

null vs undefined
Null means zero or nothing
undefined means a variable is not assigned to any value yet

line 4 to 8:
Why null behaves differently .?line 4 : null is not converted to Number 0, treated as null itself
line 5 : Obviously false, because both are different Data Types
line 6 : null is converted to 0, 0 cannot be greater than 0. So false
line 7 : null is converted to 0, 0 cannot be less than 0. So true
line 8 : null is converted to 0, 0 can be greater than or equal to 0. So true
why is it so.?
Equality check == and comparisons >, <, ≥, ≥ work differently.
Equality == does not convert null to number
Comparisons convert null to Number.
So null ≥ 0 is true , null == 0 is false
line 10 : Equality check : undefined is equal to null
line 11 : Strict Equality : both are of different Data Types
Undefined is incomparable, it does not take part in any comparisons, undefined only equals to null
What is the reason .?
Undefined gets converted to NaN, (Not a Number),which is a special numeric value returns, when a number is compared or operated with a non number.

line 4 : Shows null equals 0 in a math operation
line 6 : undefined does not have a value to take part in a math operation. So it returns NaN
After all “undefined” means undefined. Then how can it be compared or operated with others.
Now we know the difference between =, ==, ===
= For assigning a value to the variable
== Equality check for different values
=== Strict equality check (Data Type check)
Last updated
Was this helpful?