Javascript Infinity - (infinity minus minus) gives infinity
I'm looking for a JavaScript number type system.
I am using Chrome. When I evaluate 15--
for a numeric literal, I get ReferenceError
as there is no point in decrementing the constant.
When I evaluate var x=10;x--;
as expected everything works.
Expected var a=Infinity;a--
evaluates to Infinity
, this all makes sense and conforms to the javascript spec .
However, to my surprise, Infinity--
and Infinity++
are evaluated Infinity
unlike other literals.
This also happens for Number.POSITIVE_INFINITY
that is the same.
tl; dr:
Why Infinity--
gives infinity as a result when 15--
and (new Number(15))--
gives a reference error?
Infinity
as used in your example is not actually a value but refers to a Infinity
property of the global object :
15.1 Global Object
[...]
15.1.1 Global Object Value Properties
[...]
15.1.1.2 Infinity
The valueInfinity
is+∞
(see 8.5 ). This property has attributes {[[Writable]]:,false
[[Enumerable]]:,false
[[Configurable]]:false
}.
So, it Infinity--
coincides with window.Infinity--
, which is perfectly true.
Since there is no such thing as infinity of a number, this is a concept, and hence when coding it is not built like other constants, but as an object like null or undefined, but with some properties, it behaves well with mathematical methods.