Is the below if statement
if (a >= b)
Equal to this?
if (a > b || a === b)
Or is it equal to this?
if (a > b || a == b)
Is the below if statement
if (a >= b)
Equal to this?
if (a > b || a === b)
Or is it equal to this?
if (a > b || a == b)
It is equivalent to if(a > b || a == b)
var a = "2";
var b = 2;
console.log(a >= b); // true
console.log(a > b || a == b); // true (== compares value)
console.log(a > b || a === b); // false (=== compares value and type)
Actually the first one
if(a >= b)
is similiar to
if(a > b || a == b)
but not equals to
if(a > b || a === b)
because in this last one you are even comparing the type of both the operands.
Example: x = "5"
console.log(x==parseInt(x)) will return true
console.log(x===parseInt(x)) will return false
So, == does not consider the types of operands.
You can test it in the console:
var a = 0;
var b = '0';
a == b; // true
a === b; // false
a >= b; // true
Ergo, >=
is equivalent to > || ==
.
The actual result depend on the use case if the typeof
both a
& b
is same then (a >= b)
is same as (a > b || a === b)
. This is because ==
is equality with type coercion
var a = "2";
var b = "2";
console.log(a >= b); // true
console.log(a > b || a == b); // true
console.log(a > b || a === b); // true
var a = "4";
var b = 4;
console.log(a >= b); // true
console.log(a > b || a == b); // true
console.log(a > b || a === b); // false
It depends. If before the statement you had defined a or b with a different type,
if(a > b || a === b)
will return false if the first clause is not true. However if you didn't define a or b before both will have the same type and both expressions are equivalent.
You can understand "===" as
(a == b && sameType(a,b))