4.9.2 Comparison Operators
The comparison operators test the relative order (numerical or alphabetics) of their two operands:
Less than (<) The < operator evaluates to true if its first operand is less than its second operand; otherwise it evaluates to false.
Greater than (>) The > operator evaluates to true if its first operand is greater than its second operand; otherwise it evaluates to false.
Less than or equal (<=) The <= operator evaluates to true if its first operand is less than or equal to its second operand; otherwise it evaluates to false.
Greater than or equal (>=) The >= operator evaluates to true if its first operand is greater than or equal to its second operand; otherwise it evaluates to false.
The operands of these comparison operators may be of any type. Comparison can be performed only on numbers and strings, however, so operands that are not numbers or strings are converted. Comparison and conversion occur as follows:
- If either operand evaluates to an object, that object is converted to a primitive value as described at the end of §3.8.3 : if its valueOf() method returns a primitive value, that value is used. Otherwise, the return value of its toString() method is used.
- If, after any required object-to-primitive conversion, both operands are strings, the two strings are compared, using alphabetical order, where “alphabetical order” is defined by the numerical order of the 16-bit Unicode values that make up the strings.
- If, after object-to-primitive conversion, at least one operand is not a string, both operands are converted to numbers and compared numerically. 0 and -0 are considered equal. Infinity is larger than any number other than itself, and -Infinity is smaller than any number other than itself. If either operand is (or converts to) NaN, then the comparison operator always returns false.
4.9 Relational Expressions | 73
letters. This rule can cause confusing results if you do not expect it. For example, according to the < operator, the string “Zoo” comes before the string “aardvark”.
For a more robust string-comparison algorithm, see the String.localeCompare()method, which also takes locale-specific definitions of alphabetical order into account. For case-insensitive comparisons, you must first convert the strings to all lowercase or all uppercase using String.toLowerCase() or String.toUpperCase().
Both the + operator and the comparison operators behave differently for numeric and string operands. + favors strings: it performs concatenation if either operand is a string. The comparison operators favor numbers and only perform string comparison if both operands are strings:
1 + 2 // Addition. Result is 3. "1" + "2" // Concatenation. Result is "12". "1" + 2 // Concatenation. 2 is converted to "2". Result is "12". 11 < 3 // Numeric comparison. Result is false. "11" < "3" // String comparison. Result is true. "11" < 3 // Numeric comparison. "11" converted to 11. Result is false. "one" < 3 // Numeric comparison. "one" converted to NaN. Result is false.欢迎转载,转载请注明来自一手册:http://yishouce.com/book/1/31327.html