DZone
Web Dev Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Web Dev Zone > NaN and Infinity in JavaScript

NaN and Infinity in JavaScript

Axel Rauschmayer user avatar by
Axel Rauschmayer
·
Feb. 13, 12 · Web Dev Zone · Interview
Like (0)
Save
Tweet
7.44K Views

Join the DZone community and get the full member experience.

Join For Free
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

JavaScript

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • MACH Architecture Explained
  • Is Your Code DRY or WET?
  • 5 Best JavaScript Web Development Frameworks
  • Practice on Pushing Messages to Devices of Different Manufacturers

Comments

Web Dev Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo