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.

CHAPTER 2 ■ TYPE CONVERSION<br />

Note too that, if either operand to a math operator like * or - is NaN, the return value will be NaN, too.<br />

Therefore, as Figure 2–22 displays, you can do math with null but not with undefined.<br />

var nothing, zilch = null;<br />

nothing * 4;<br />

// NaN<br />

zilch * 4;<br />

// 0<br />

Figure 2–22. JavaScript can do math with null but not with undefined.<br />

Converting strings to numbers is fairly straightforward. Number-like strings such as "4" or "3.33"<br />

convert to an equivalent number (4 and 3.33). The "" empty string converts to 0. Everything else<br />

converts to NaN. Let’s try converting a few strings to a number in Firebug, verifying our work with Figure<br />

2–23:<br />

Number("4");<br />

// 4<br />

Number("");<br />

// 0<br />

Number("Mr. Incredible");<br />

// NaN<br />

47

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

Saved successfully!

Ooh no, something went wrong!