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.

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.

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

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.

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?