Difference between == and === operators

Difference between == and === operators

Let's dive deep to see how these comparison operators work in different cases

IsLooselyEqual (==) operator

Let's assume two values are given, the first value as A and the second value as B. Now under different scenarios, we can get -

If A and B are of the same type. Then, it compares values strictly and in other cases, it will compare loosely

If A is null and B is undefined or vice versa, it returns true

If A is a string and B is a number(int or BigInt) or vice versa, It will convert the string to a number(int or BigInt) and will perform the comparison

If A or B any value is boolean, it will return true if both of them are true( or truthy values) or false(or falsy values) or one of them is true and the other one is number 1 or one of them is false and the other one of them is 0. Otherwise, all cases will return false.

While we are comparing two objects (non-primitive type). Then it will always give false as A is pointing towards a certain memory location which cannot be same as B. But if another variable(C) takes the same reference as A or B, then it will give true when compared(A == C).

If A and B both of them have infinity, then it will return true. If one of them has, it will return false.

IsStrictlyEqual (===) operator

Let's assume two values are given, the first value as A and the second value as B.
Now,

If both A and B are of the same type it will give true and false otherwise.

If A have null and B has undefined or vice versa, it will return false