Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Infinity vs Number.POSITIVE_INFINITY

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?

like image 384
zzzzBov Avatar asked Dec 15 '11 15:12

zzzzBov


People also ask

What is the value of number NEGATIVE_INFINITY?

The value of Number. NEGATIVE_INFINITY is the same as the negative value of the global object's Infinity property. This value behaves slightly differently than mathematical infinity: Any positive value, including POSITIVE_INFINITY , multiplied by NEGATIVE_INFINITY is NEGATIVE_INFINITY .

Is infinity a number JS?

Description. Infinity is a property of the global object. In other words, it is a variable in global scope. The initial value of Infinity is Number.

What is negative infinity?

The negative infinity in JavaScript is a constant value which is used to represent a value which is the lowest available. This means that no other number is lesser than this value.

How do you get positive infinity?

We get positive infinity when we divide positive infinity by any positive number (except positive infinity) Positive infinity divided by either positive or negative infinity is NaN.


1 Answers

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.

like image 133
pimvdb Avatar answered Sep 18 '22 16:09

pimvdb