AJAX - The Complete Reference

(avery) #1

PART IV


Appendix A: JavaScript Quick Reference 573


Relational Operators


Relational operators, as detailed in Table A-17, are binary operators that compare two like
types and evaluate to a Boolean, indicating whether the relationship holds. If the two
operands are not of the same type, type conversion is carried out so that the comparison can
take place (see the section immediately following for more information).

Type Conversion in Comparisons
A JavaScript implementation should carry out the following steps in order to compare two
different types:


  1. If both of the operands are strings, compare them lexicographically.

  2. Convert both operands to numbers.

  3. If either operand is NaN, return undefined (which in turn evaluates to false when
    converted to a Boolean).

  4. If either operand is infinite or zero, evaluate the comparison using the rules that +0
    and –0 compare false unless the relation includes equality, that Infinity is never
    less than any value, and that –Infinity is never more than any value.

  5. Compare the operands numerically.


NNOT EOTE Using the strict equality (===) or inequality (!==) operator on operands of two different
types will always evaluate false.

Lexicographic Comparisons
The lexicographic comparisons performed on strings adhere to the following guidelines.
Note that a string of length n is a “prefix” of some other string of length n or more if they
are identical in their first n characters. So, for example, a string is always a prefix of itself.


  • If two strings are identical, they are equal (note that there are some very rare
    exceptions when two strings created using different character sets might not
    compare equal, but this almost never happens).

  • If one string is a prefix of the other (and they are not identical) then it is “less than”
    the other. For example, “a” is less than “aa.”


Operator Description
< Evaluates true if the first operand is less than the second
<= Evaluates true if the first operand is less than or equal to the second
> Evaluates true if the first operand is greater than the second
>= Evaluates true if the first operand is greater than or equal to the second
!= Evaluates true if the first operand is not equal to the second
== Evaluates true if the first operand is equal to the second
!== Evaluates true if the first operand is not equal to the second (or they don’t have the
same type)
=== Evaluates true if the first operand is equal to the second (and they have the same type)

TABLE A-17 Relational Operators
Free download pdf