14

alert(1/0) alerts Infinity and alert(1/-0) alerts -Infinity. alert(-1/-0) alerts Infinity, as I could expect when doing some operations with real numbers. I can't say that infinity is a measurable value. Does javascript think it is some number?

nicael
  • 18,550
  • 13
  • 57
  • 90
  • Do you think you can divide by zero? Yes, Infinity get treated like a number: `1.7976931348623157E+10308`. – A1rPun Jun 11 '14 at 15:05
  • 2
    Googling "javascript infinity" would have answered this and then some https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Infinity – user229044 Jun 11 '14 at 15:05

3 Answers3

20

Yes, Infinity and -Infinity are special values of the Number type. From the ES5 spec:

There are two other special values, called positive Infinity and negative Infinity. For brevity, these values are also referred to for expository purposes by the symbols +∞ and −∞, respectively. (Note that these two infinite Number values are produced by the program expressions +Infinity (or simply Infinity) and -Infinity.)

Also note that NaN is a value of the Number type too, despite it being an acronym for "not a number".

James Allardice
  • 164,175
  • 21
  • 332
  • 312
7

JavaScript uses IEEE-754 to represent numerical types; that specification includes values for non-numbers such as +/-Infinity and "NaN".

(1/0) // => Infinity
typeof(Infinity) // => "number"

Number.POSITIVE_INFINITY ===  Infinity // => true
Number.NEGATIVE_INFINITY === -Infinity // => true

Arithmetic and logical operations including the infinite values should behave as expected.

maerics
  • 151,642
  • 46
  • 269
  • 291
1

From the Mozilla docs:

The initial value of Infinity is Number.POSITIVE_INFINITY. The value Infinity (positive infinity) is greater than any other number including itself. This value behaves mathematically like infinity; for example, any positive number multiplied by Infinity is Infinity, and anything divided by Infinity is 0.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Infinity

Tom
  • 7,640
  • 1
  • 23
  • 47