1

I accidentally faced an Infinity property in JavaScript and wondered where in the world it can be used? Any real life example please.

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
Vladimirs
  • 8,232
  • 4
  • 43
  • 79

3 Answers3

4

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);
Stefan Falk
  • 23,898
  • 50
  • 191
  • 378
  • 4
    `Math.min.apply` should be the choice. – VisioN May 01 '14 at 11:53
  • 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
  • 1
    The 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
2

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.

VisioN
  • 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
1

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.

Qantas 94 Heavy
  • 15,750
  • 31
  • 68
  • 83