Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Equal but not greater-or-equal?

Сomparing anything with undefined has been discussed many times, but today I had to compare items in arrays and, at some point, items in the array could be undefined and I was curious how they will compare to each other. I found that undefined == undefined is true, but at the same time undefined >= undefined is not true. Wait a second, if something is equal, then greater-or-equal also mean to be true? That is how logic works!

I can accept anything that ECMA Standard is saying, I believe those guys are sane and have a good reason for everything. But what on the earth could be a reason for this comparison behavior? If I was implementing undefined I would return undefined for any comparison that has undefined as an operand. But they choose to return boolean, and then why the results are like this?

Now I think the best solution is just to handle any appearance of undefined in a special way with multiple of if statements.

    undefined === undefined: true
    undefined == undefined: true

    undefined !== undefined: false
    undefined != undefined: false
    undefined < undefined: false
    undefined > undefined: false
    undefined <= undefined: false
    undefined >= undefined: false
like image 552
exebook Avatar asked Dec 02 '13 06:12

exebook


People also ask

What is the symbol for not greater or equal to?

The notation a ≤ b or a ⩽ b means that a is less than or equal to b (or, equivalently, at most b, or not greater than b).

What is less than greater than and equal?

The greater than symbol means the number on the left is greater than the number on the right. The greater than or equal symbol means the number on the left is greater than or equal to the number on the right. The less than symbol means that the number on the left is less than the number on the right.

What does ≤ mean?

The symbol ≤ means less than or equal to. The symbol ≥ means greater than or equal to.

What is less than or equal to called?

The less than symbol is <. Two other comparison symbols are ≥ (greater than or equal to) and ≤ (less than or equal to).


1 Answers

But what on earth could be the reason for this comparison behavior?

The ECMAScript specification was not written to describe how JavaScript should work, it was written to describe how JavaScript actually works.

The language was implemented rapidly in Netscape Navigator, and contained many "bugs" such as this, which were then copied in IE's implementation. Eventually, ECMAScript was standardized, but not until after JavaScript existed in the wild. Standardizing any language that's already being used requires a certain amount of backwards compatibility.

<= and >= diverge from == simply because the standard says so. The standard says so because that was how they were implemented initially.

like image 163
zzzzBov Avatar answered Oct 19 '22 02:10

zzzzBov