4

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)
Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
Red
  • 6,599
  • 9
  • 43
  • 85
  • 1
    The === is also compare the value and type, otherwise the == compare the value. For me is equal if(a > b || a == b) – mbadeveloper May 23 '18 at 12:27
  • @Andrew `0`, `false`. That's why `==` and `===` exist separately in the first place. – deceze May 23 '18 at 12:28
  • 1
    @AndrewMorton `a = 0` and `b = '0'` – Gabriel Carneiro May 23 '18 at 12:28
  • @Andrew The result is different depending on whether `==` or `===` is used. I fail to see the point you're trying to make. – deceze May 23 '18 at 12:30
  • The JavaScript equality table may help to understand the difference https://dorey.github.io/JavaScript-Equality-Table/ – tk3 May 23 '18 at 12:36
  • @AndrewMorton I was comparing (3 >= "3") and was wondering if the `=` was standing for `==` or `===`. I couldn't really found an awnser. But the duplicate fully awnsers my question. Thanks everyone. – Red May 23 '18 at 12:39

5 Answers5

9

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)
Nikhil Aggarwal
  • 28,197
  • 4
  • 43
  • 59
  • 1
    This isn't true when comparing objects, please consider the following example: `let obj1 = {a: 1}; let obj2 = {a: 1};` `obj1 >= obj2` would return true, but `obj1 > obj2 || obj1 == obj2` would return false ! (same thing when comparing `0` and `null`) – Hamza El Aoutar May 23 '18 at 12:36
  • And if a and b are the same type, it is also equivalent to the === version. Hence it is not equivalent to one or another exclusively. Hence it depends. – Jorge.V May 23 '18 at 12:42
  • @Jorge.V - In case of same type `==` is also true, hence, it becomes super set of same and different types – Nikhil Aggarwal May 23 '18 at 12:44
  • @NikhilAggarwal that is correct. And it doesn't change the fact that it depends, because it is also equivalent to === in some cases. By ignoring this I think you are missleading. – Jorge.V May 23 '18 at 12:45
  • @ElAoutarHamza - Normally, we use relational operators for primitives and dates. Hence, the example seems to be breaking but it should not be used at first place – Nikhil Aggarwal May 24 '18 at 12:37
0

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.

Aniruddh Agarwal
  • 900
  • 1
  • 7
  • 22
0

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 > || ==.

Máté Safranka
  • 4,081
  • 1
  • 10
  • 22
0

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
brk
  • 48,835
  • 10
  • 56
  • 78
-3

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))
Jorge.V
  • 1,329
  • 1
  • 13
  • 19
  • No, it does *not* depend on anything whether `>=` acts as `==` or `===`. – deceze May 23 '18 at 12:31
  • He isn't asking whether >= acts as == or ===, but if it is equivalent to one expression or the other. And it depends on what I said. – Jorge.V May 23 '18 at 12:34