JS Operators Part Two
Last updated
Was this helpful?
Last updated
Was this helpful?
Comparison operators are used to compare two values in a Boolean pattern
Comparison returns a logical value (Boolean)
True (or) False
Check for the corresponding line number in the console for readability.
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
In the case of Boolean and Numbers,
True becomes 1 and False becomes 0
line no.11 is an empty string used for readability.
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.
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β .?
β===β 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.
!==, Does the non-equality check for the Data Type
Null means zero or nothing
undefined means a variable is not assigned to any value yet
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
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.
After all βundefinedβ means undefined. Then how can it be compared or operated with others.
= For assigning a value to the variable
== Equality check for different values
=== Strict equality check (Data Type check)