11.12.2012 Views

JavaScript 2.0-The Complete Reference, Second ... - freecodingtutorial

JavaScript 2.0-The Complete Reference, Second ... - freecodingtutorial

JavaScript 2.0-The Complete Reference, Second ... - freecodingtutorial

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.

<strong>The</strong> other important special value is NaN, which means ―not a number.‖ Numeric data takes on<br />

this value when it is the result of an undefined operation. Common examples of operations that<br />

result in NaN are dividing zero by zero, taking the sine of Infinity, and attempting to add<br />

Infinity to –Infinity. <strong>The</strong> NaN value is also sticky, but unlike the infinite values it never<br />

compares equal to anything. Because of this, you must use the isNaN() method or compare the<br />

value to itself to determine if a value is NaN. <strong>The</strong> isNaN() method returns a Boolean indicating<br />

whether the value is NaN. This method is so important that it is a property of the Global object,<br />

so it can be called directly in your scripts. Comparing the value to itself will indicate whether the<br />

value is NaN because it is the only value that does not compare equal to itself!<br />

<strong>The</strong> following example illustrates the use of both techniques:<br />

var x = 0 / 0; // assign NaN to x<br />

if (x != x) // check via self-equality<br />

{<br />

// do something<br />

}<br />

if (isNaN(x)) // check via explicit call<br />

{<br />

// do something<br />

}<br />

Table 3-1 summarizes these special types.<br />

Table 3-1: Summary of Special Numeric Data Values<br />

Special Value Result of Comparisons Sticky?<br />

Infinity, –Infinity Number too large or<br />

small to be represented<br />

All Infinity values<br />

compare equal to<br />

each other<br />

NaN Undefined operation NaN never<br />

compares equal to<br />

anything, even itself<br />

Yes<br />

Yes

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

Saved successfully!

Ooh no, something went wrong!