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?
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 .
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.
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.
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.
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 15.1.1.2
InfinityThe initial value of
Infinityis+∞.15.7.3.6
Number.POSITIVE_INFINITYThe value of
Number.POSITIVE_INFINITYis+∞.This property shall have the attributes { DontEnum, DontDelete, ReadOnly }.
In ES5, the value properties of the global object were made read only:
15.1.1.2
InfinityThe value of
Infinityis+∞(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_INFINITYThe value of
Number.POSITIVE_INFINITYis+∞.This property has the attributes
{ [[Writable]]:false, [[Enumerable]]: false, [[Configurable]]: false }.
As of ES2018 these definitions have not changed.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With