For < operator, it seems very easy to understand when it applies to two strings, two numbers and two booleans.
My question has two parts:
What is the rule when we use it to compare two objects?
It seems that I cannot write {} < {}, but the following code generate false.
var a = {};
var b = {};
console.log(a < b); //false
What is the rule when we use it to compare two different type?
true < "" //false
true > "" //true
10 < true //false
10 < "" //false
I don't know why the results are like these. I found an article on comparison in JavaScript but there is no detail.
BTW, I have no question on how == and === work.
What is the rule when we use it to compare two objects?
From wikipedia:
When comparing variables which are objects they are considered to be different if their objects are not the same object, even if the values of them are the same, so:
var obj1 = {a: 1};
var obj2 = {a: 1};
var obj3 = obj1;
alert(obj1 == obj2); //false
alert(obj3 == obj1); //true
What is the rule when we use it to compare two different type?
It is explained in the article you posted, from which I quote:
If the two operands are not of the same type, JavaScript converts the operands, then applies strict comparison. If either operand is a number or a boolean, the operands are converted to numbers if possible; else if either operand is a string, the string operand is converted to a number if possible. If both operands are objects, then JavaScript compares internal references which are equal when operands refer to the same object in memory.
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