More specifically, greater-than-or-equal-to operations.
Logically n >= k
should be equal to n > k || n == k
but that doesn't seem to be the case.
Why is it that this:
var d1 = new Date(2018, 1, 16);
var d2 = new Date(2018, 1, 16);
console.log(d1 > d2);
console.log(d1 < d2);
console.log(d1 == d2);
console.log(d1 >= d2);
console.log(d1 <= d2);
produces false
, false
, false
, true
, true
?
In JavaScript they are written like this: Greater/less than: a > b , a < b . Greater/less than or equals: a >= b , a <= b . Equals: a == b , please note the double equality sign == means the equality test, while a single one a = b means an assignment.
The greater than operator ( > ) returns true if the left operand is greater than the right operand, and false otherwise.
The less than or equal operator ( <= ) returns true if the left operand is less than or equal to the right operand, and false otherwise.
console.log(d1 > d2);
console.log(d1 < d2);
These convert them to numbers first, then compare them. As they are at the same time, they got the same number so one is not bigger or smaller than the other.
console.log(d1 == d2);
This checks if the date references are the same. But they are not as they are two different objects.
console.log(d1 >= d2);
console.log(d1 <= d2);
These compare them by numbers, but also for equality. If you do:
console.log(+d1 === +d2);
you see that they are equal by the number they represent.
Reference: == <=
TLDR: use ===
and manually convert types to prevent such odd behaviour...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With