29

I understand that Number.POSITIVE_INFINITY has a value of Infinity, and Number.NEGATIVE_INFINITY has a value of -Infinity.

Is there a reason I would use Number.POSITIVE_INFINITY instead of Infinity, or Number.NEGATIVE_INFINITY instead of -Infinity?

On a related note, are there any cross-browser issues with isFinite?

zzzzBov
  • 174,988
  • 54
  • 320
  • 367

1 Answers1

32

TL;DR

Infinity used to be overwritable; Number.POSITIVE_INFINITY and Number.NEGATIVE_INFINITY have always been read only.


Infinity is a property of the global object (window is the global object for Javascript run in the browser), whereas Number.POSITIVE_INFINITY is a property of the Number constructor.

Prior to the 5th Edition of ECMAScript, value properties of the global object were able to be overwritten:

Infinity = 123;
Infinity; // 123

The same applies to undefined and NaN, which are also properties of the global object and used to be overwritable.

Properties of the Number constructor have always been read only:

Number.POSITIVE_INFINITY = 123;
Number.POSITIVE_INFINITY; // Infinity

Specs:

ECMAScript 1st Edition (June 1997)

15.1.1.2 Infinity

The initial value of Infinity is +∞.

15.7.3.6 Number.POSITIVE_INFINITY

The value of Number.POSITIVE_INFINITY is +∞.

This property shall have the attributes { DontEnum, DontDelete, ReadOnly }.

ECMAScript 5th Edition (December 2009)

In ES5, the value properties of the global object were made read only:

15.1.1.2 Infinity

The value of Infinity is +∞ (see 8.5).

This property has the attributes { [[Writable]]: false, [[Enumerable]]: false, [[Configurable]]: false }.

The properties of the Number constructor didn't really change, but the attributes were renamed:

15.7.3.6 Number.POSITIVE_INFINITY

The value of Number.POSITIVE_INFINITY is +∞.

This property has the attributes { [[Writable]]: false, [[Enumerable]]: false, [[Configurable]]: false }.

As of ES2018 these definitions have not changed.


About isFinite:

I once posted a question as to why the Google Closure Library implements a custom function for isFinite, and the answer was that there was probably some cross-browser inconsistency, although it's unclear which browser and which inconsistency.

Community
  • 1
  • 1
pimvdb
  • 151,816
  • 78
  • 307
  • 352
  • 2
    But, you didn't really answer whether `Number.POSITIVE_INFINITY` gives the same result as `Infinity` other than the fact that `Infinity` is changeable in older browsers. – jfriend00 Dec 15 '11 at 15:56
  • 2
    @jfriend00: That's not the question if I'm understanding it correctly. It is asking whether there is a reason for using `Number.POSITIVE_INFINITY` over `Infinity`, and this reason is a legitimate one I think. – pimvdb Dec 15 '11 at 15:58
  • I thought one of the reason you might use one over the other is if they were actually different in some way (e.g. behaved differently in some circurmsntaces). – jfriend00 Dec 15 '11 at 19:15
  • 3
    They behave differently if `Infinity` has been overwritten :) – pimvdb Dec 16 '11 at 11:10