I accidentally faced an Infinity property in JavaScript and wondered where in the world it can be used? Any real life example please.
-
Can think of only 1/0 == Infinity but that does no sense where it can be used. – Vladimirs May 01 '14 at 11:47
-
It's the minimum value in an empty array of numbers – Bergi May 01 '14 at 11:48
-
Cant agree that my question is too broad. I mentioned "Any real life example please." – Vladimirs May 01 '14 at 11:50
-
5I don't get why people downvote this, it seems as a legitemate question and it made me curious too. upvote – Alexandru Severin May 01 '14 at 11:51
-
1One possible usage example: http://stackoverflow.com/a/12900504/1249581. – VisioN May 01 '14 at 11:51
-
A cool use is `.flat(Infinity)` to deep flatten any array of arrays – CH4B Aug 10 '20 at 16:44
3 Answers
You can use it if you don't know what the minimum value of an array
or also a mathematical-function
is like this:
var minimum = Infinity;
var i = 0;
for(i = 0; i < array.length; i++) {
if(array[i] < minimum) {
// new minimum found
minimum = array[i];
}
}
alert("Minimum: " + minimum);

- 23,898
- 50
- 191
- 378
-
4
-
This is just an example how `Infinity` can be and is used. What answer would that be: "*Use library function X*." ? – Stefan Falk May 01 '14 at 11:54
-
I asked about real life example I can write a whole bunch of code where I can put Infinity. Ideally I wanted to see example where I cant go without Infinity. Because for me it looks like just useless property. – Vladimirs May 01 '14 at 11:56
-
1The term *real life example* is kind of bad chosen. `Infinity` is useful in such cases I showed you and it also can be helpful as indicator for mathematical functions which produce figures which can not be stored in variables. In many cases e.g. `elliptic curve cryptography` you will make use of that property and I am sure there are many other examples out there like `ecc`. So what is your point anyway? – Stefan Falk May 01 '14 at 12:03
-
@Vladimirs see my edit. but you might want to read something about ECC yourself since I am not familiar with the mathematical background. – Stefan Falk May 01 '14 at 12:16
Here is another real life example:
var x = +prompt('Enter x:'),
y = +prompt('Enter y:'),
value = Math.pow(x, y);
if (value === Infinity || value === -Infinity) {
console.log('The value is too large!');
}
As an example, if the entered values are 1e100
and 100
, the power method returns Infinity
.

- 143,310
- 32
- 282
- 281
-
Number.isFinite() is better to use there, but seems it just a shortcut to your condition – Vladimirs May 01 '14 at 13:33
-
1@Vladimirs Yeah, but if you need to separate the positive and negative conditions `isFinite` won't really work. Moreover, calling a method in the condition is always slower than checking against the value. – VisioN May 01 '14 at 13:36
I'm assuming you're asking about why there's an Infinity
global property, not why there's a concept of having infinities in the first place, which is another matter.
It allows easy comparison with the Infinity
value itself, where you get it from some arithmetic:
function inv(x) {
return x * 100000000000;
}
inv(1e999) === Infinity;
This is especially useful as 1 / 0
is not equal to Infinity
in mathematics, so it's not obvious that you can use 1 / 0
.
If you want a numeric comparison to always return true, and you're using a variable, you can set the variable to Infinity to always force the condition to be true. Take this example:
var a = Infinity; // some number from elsewhere
var arr = [];
function foo(maxLen) {
if (arr.length < maxLen) arr.push(1);
}
foo(a); // you can't change the function
This could be useful in cases where you can't change the comparison statement used.

- 15,750
- 31
- 68
- 83
-
2
-
@Vladimirs: `true` does not do the same thing, if the other side is `2` that's useless. – Qantas 94 Heavy May 01 '14 at 11:49
-
This does not make sense OneKitten. It would be equal to write `function foo(x) { alert(1); }` though I am not sure what `if(Infinity > Infinity)` would result in. – Stefan Falk May 01 '14 at 11:56
-
1@StefanR.Falk: that was just meant to be an example, I thought that it was obvious that `a` wouldn't always be `Infinity` and would come from some external source. – Qantas 94 Heavy May 01 '14 at 11:57