04.11.2015 Views

javascript

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 3: Language Basics<br />

❑<br />

❑<br />

❑<br />

If one operand is a number, convert the other operand to a number and perform a numeric<br />

comparison.<br />

If an operand is an object, call valueOf() and use its result to perform the comparison<br />

according to the previous rules. If valueOf() is not available, call toString() and use that<br />

value according to the previous rules.<br />

If an operand is a Boolean, convert it to a number and perform the comparison.<br />

When a relational operator is used on two strings, an interesting behavior occurs. Many expect that less -<br />

than means “ alphabetically before ” and greater - than means “ alphabetically after, ” but this is not the<br />

case. For strings, each of the first string ’ s character codes is numerically compared against the character<br />

codes in a corresponding location in the second string. After this comparison is complete, a Boolean<br />

value is returned. The problem here is that the character codes of uppercase letters are all lower than the<br />

character codes of lowercase letters, meaning that you can run into situations like this:<br />

var result = “Brick” < “alphabet”; //true<br />

In this example, the string “Brick” is considered to be less than the string “alphabet” because the<br />

letter B has a character code of 66 and the letter a has a character code of 97. To force a true alphabetic<br />

result, you must convert both operands into a common case (upper or lower) and then compare like this:<br />

var result = “Brick”.toLowerCase() < “alphabet”.toLowerCase(); //false<br />

Converting both operands to lowercase ensures that “alphabet” is correctly identified as alphabetically<br />

before “Brick” .<br />

Another sticky situation occurs when comparing numbers that are strings, such as in this example:<br />

var result = “23” < “3”; //true<br />

This code returns true when comparing the string “23” to “3” . Because both operands are strings, they<br />

are compared by their character codes (the character code for “2” is 50; the character code for “3” is 51).<br />

If, however, one of the operands is changed to a number as in the following example, the result makes<br />

more sense:<br />

var result = “23” < 3;<br />

//false<br />

Here, the string “23” is converted into the number 23 and then compared to 3, giving the expected<br />

result. Whenever a number is compared to a string, the string is converted into a number and then<br />

numerically compared to the other number. This works well for cases like the previous example, but<br />

what if the string can ’ t be converted into a number? Consider this example:<br />

var result = “a” < 3;<br />

//false because “a” becomes NaN<br />

The letter “a” can ’ t be meaningfully converted into a number, so it becomes NaN . As a rule, the result of<br />

any relational operation with NaN is false , which is interesting when considering the following:<br />

var result1 = NaN < 3;<br />

var result2 = NaN > = 3;<br />

//false<br />

//false<br />

59

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!