23.04.2013 Views

javascript

javascript

javascript

SHOW MORE
SHOW LESS

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

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

Figure 3–13. Determining whether one number is greater than another with the > operator<br />

CHAPTER 3 ■ OPERATORS<br />

In addition to comparing numbers, > is sometimes used to compare strings. However, it does so<br />

numerically by the Unicode encoding for each character. Uppercase letters are greater than lowercase<br />

characters. Therefore, you want to invoke toLowerCase() on both operands to get an alphabetical<br />

comparison as Listing 3–1 displays. Note that we explored toLowerCase() and other string methods in<br />

Chapter 2.<br />

Listing 3–1. Comparing Strings Alphabetically with the > Operator<br />

"apple" > "Granny Smith";<br />

// false<br />

"apple".toLowerCase() > "Granny Smith".toLowerCase();<br />

// true<br />

Determining Whether One Number or String Is Less Than Another<br />

Now what if we want to do the inverse? In other words, we want to determine whether there’s less pure<br />

maple syrup than ground pecans in the dough.<br />

Hmm.<br />

Want to take a guess?<br />

Yup, turn the > around and you have the < less than operator, which tells you whether its first<br />

operand is less than its second operand. Let’s muck around with < in Firebug, verifying our work with<br />

Figure 3–14.<br />

var cake = {<br />

dough: {<br />

organicPastryFlour: [1 + 1/2, "cup"],<br />

freshlyGroundNutmeg: [1/4, "tsp"],<br />

77

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

Saved successfully!

Ooh no, something went wrong!