Comparison operators>, <, >=, <= in JavaScript, and javascript Operators
Like the = Operator, comparison operators (>,<>=, <=) can convert objects to string or number before comparison -- for number, compare the value size; for string, compare the order of Characters in the encoding table. Unlike the = Operator, = converts the Date object to a string first and then compares it, the comparison operator converts all objects including Date into numbers first and then compares them. The comparison rules are as follows:
1. If there is an object on both sides of the operator, convert it to number. If it cannot be converted to number, convert it to string.
2. After conversion, if both sides of the operator are strings, the string is compared; otherwise, the value is compared as long as number appears on one side.
3. If NaN appears on both sides of the operator, false is returned.
4.0 is equal to-0.
Lab
Copy codeThe Code is as follows:
// In comparison, Date object is converted to number
Var d = new Date ();
Var s1 = "Thu Mar 27 2008 14:57:11 GMT + 0800 (CST )";
Var s2 = "Thu Mar 27 2099 14:57:11 GMT + 0800 (CST )";
Var n1 = d. valueOf ()-1000;
Var N2. = d. valueOf () + 1000;
Console. log (d> s1); // false, d is converted to number, and that number is further converted to string. It is a string comparison here.
Console. log (d> s2); // false
Console. log (d> n1); // true
Console. log (d> n2); // false
Console. log ("11"> 3); // true