πŸ“’
SDV503
  • SDV503
  • Command Prompt
    • Windows Command Prompt
    • Mac OS Terminal
  • GIT & GITHub
    • GitHub
    • Git
      • Git Workflow
        • Forking Workflow
  • README
    • How to write a readme.md file?
    • Write a better README.me
    • Generate a README for GitHub Repo Quickly
  • Code-comments
    • Clean Comments
    • Writing Comments in a Good way
  • Pair Coding
    • What is Pair Coding?
    • Pair Programming Experience
  • Programming?
    • What is Programming?
    • What Is A Programming Paradigm?
    • JavaScript Programming Paradigms
  • Number Systems
    • Decimal and Binary numbers
  • JavaSCript
    • Introduction To JavaScript
      • The JavaScript Engine
  • JS Call Stack
    • JavaScript call stack
    • JavaScript & Memory
      • Memory Leaks in JavaScript
    • Execution Context and Execution Stack in Javascript
      • Javascript Execution Context and Hoisting
  • JavaScript Variables
    • Introduction to JS Data Types
    • Primitive and Non-Primitive
    • Operator precedence and associativity
      • JS Operators Part One
      • JS Operators Part Two
      • JS Operators Part Three
    • Clean Code for JS Variables
  • JavaScript Scopes
    • Scope (Chain) Visualized
  • Javascript β€Šβ€”β€Šthis keyword
  • JavaScript Data Types
    • More JavaScript Data Types
  • JavaScript Expressions and Statements
  • if/else, switch and ternary operator
    • If/Else statement
  • Switch Statement
  • Ternary Operator
  • JavaScript Loops
    • Loops in JavaScript
      • Trouble With Loops
  • Objects
    • JavaScript Objects
      • Prototypal Inheritance Visualized
      • JavaScript Number Object
      • JavaScript String Object
  • Functions
    • JavaScript Function Part One
    • JavaScript Function Part Two
    • Immediately Invoked Function Expressions ~ IIFE
    • JS => Arrow Functions
    • JavaScript Callback
    • Hoisting in JavaScript
      • Hoisting Visualized
    • Recursion Functions
    • Curry and Function Composition
  • JavaScript Features
    • JSpread Operator
    • JavaScript Built-in Functions & Objects
  • Data Structures&Algorithms
    • JavaScript’s Data Types
    • Data Structures in JavaScript
      • Introduction to Big O Notation
      • Big O Notation in Javascript
      • Linked Lists
        • Linked Lists β€” 2
      • Hash Tables
      • Stack & Queue
  • TLDR
    • Single quotes (β€˜ ’) and double quotes (β€œ ”) in JavaScript
  • ES6
    • Generators and Iterators
    • Javascript Classes
    • JavaScript closures
    • JavaScript Promises & Async/Await
      • Event Loop Visualized
  • C#
    • What does C#? (C Sharp)
    • C# vs JavaScript
    • What Is The Difference Between C#, .NET, ASP.NET, Microsoft.NET, and Visual Studio?
    • What is the .NET Framework?
    • Methods and Properties of Console Class in C#
    • Datatypes in C#
    • C# Code Comments
    • The if statement
    • The switch statement
    • Loops
    • Comparison operators
    • Addition assignment operators
    • The String Interpolation Operator
    • Arrays
    • Lists
    • Dictionaries
Powered by GitBook
On this page
  • 3. Comparison Operator
  • Result of Comparison
  • Comparison of different types
  • Boolean and Number Comparison
  • Unicode order
  • Strict Equality
  • β€œ== β€œand β€œ===”
  • 1. ==
  • 2. ===
  • Strict non-equality
  • null vs undefined
  • Now we know the difference between =, ==, ===

Was this helpful?

  1. JavaScript Variables
  2. Operator precedence and associativity

JS Operators Part Two

PreviousJS Operators Part OneNextJS Operators Part Three

Last updated 4 years ago

Was this helpful?

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)