Did you know? DZone has great portals for Python, Cloud, NoSQL, and HTML5!
HTML5 Zone is brought to you in partnership with:

Dr. Axel Rauschmayer is a freelance software engineer, blogger and educator, located in Munich, Germany. Axel is a DZone MVB and is not an employee of DZone and has posted 228 posts at DZone. You can read more from them at their website. View Full User Profile

NaN and Infinity in JavaScript

02.13.2012
Email
Views: 1494
  • submit to reddit
The HTML5 Microzone is presented by DZone and Microsoft to bring you the most interesting and relevant content on emerging web standards.  Experience all that the HTML5 Microzone has to offer on our homepage and check out the cutting edge web development tutorials on Script Junkie, Build My Pinned Site, and the HTML5 DevCenter.
Update: New section “Detecting NaN”.


This post looks at two special values that can be the result of operations that normally return numbers: NaN and Infinity.

1. NaN

The value NaN in JavaScript stands for “not a number”. It mainly indicates that parsing a string has gone wrong:

> Number("xyz")
NaN

NaN has some Koan-like qualities. Its name is “not a number”, but it’s also not not a number (triggered by a tweet by Ariya Hidayat):

> NaN !== NaN
true

Yet, its type is “number”.

> typeof NaN
'number'

1.1 Detecting NaN

NaN is the only JavaScript value that is not equal to itself. Without equality at your disposal, you have to use the global function isNaN() to detect it.

> isNaN(NaN)
true

Kit Cambridge (via Mathias Bynens) points out a pitfall of isNaN(): It coerces its argument to number and will thus even return true for strings that cannot be converted to numbers:

> Number("xyz")
NaN
> isNaN("xyz")
true

For the same reason, isNaN will also return true for many objects:

> Number({})
NaN
> isNaN({})
true

> Number(["xzy"])
NaN
> isNaN(["xzy"])
true

Consult this previous post for details on the conversion algorithm. It is possible to override valueOf to control the result of the conversion to number:

> var obj = { valueOf: function () { return NaN } };
> Number(obj)
NaN
> isNaN(obj)
true

Cambridge’s suggested work-around is to exploit the fact that NaN is the only value x that is non-reflexive (x !== x):

function myIsNaN(x) {
    return x !== x;
}

A fixed version of isNaN will probably be added to ECMAScript 6 as Number.isNaN(). Crockford’s specification of that function better reveals what one is trying to do than Cambridge’s version. It looks as follows (simplified for explanatory purposes):

Number.isNaN = function (value) {
    return typeof value === 'number' && isNaN(value);
};

2. Infinity

Division by 0 gives you another special value:

> 3/0
Infinity

You can’t play positive and negative infinity against each other:

> Infinity - Infinity
NaN

It also turns out that “beyond infinity” is still infinity:

> Infinity + Infinity
Infinity

> 5 * Infinity
Infinity



Source: http://www.2ality.com/2012/02/nan-infinity.html

Published at DZone with permission of Axel Rauschmayer, author and DZone MVB.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

HTML5 is the most dramatic step in the evolution of web standards. It incorporates features such as geolocation, video playback and drag-and-drop. HTML5 allows developers to create rich internet applications without the need for third party APIs and browser plug-ins.  Under the banner of HTML5, modern web standards such as CSS3, SVG, XHR2, WebSockets, IndexedDB, and AppCache are pushing the boundaries for what a browser can achieve using web standards.  This Microzone is supported by Microsoft, and it will delve into the intricacies of using these new web technologies and teach you how to make your websites compatible with all of the modern browsers.