DEV Community

Cover image for The importance of using 'Infinity' on your JavaScript code.
Jesus Jimenez Cordero
Jesus Jimenez Cordero

Posted on • Edited on

The importance of using 'Infinity' on your JavaScript code.

Sometimes you need to have the biggest number to do some comparations in your code for some conditionals that can not be solved just using a specific number, so in this case, we have a solution for that and it is so simple Infinity reserved word on JavaScript. But first of all, what it means Infinity on JavaScript?

A number reaches Infinity when it exceeds the upper limit for a number: 1.797693134862315E+308

And it is the same case for negative numbers: -1.797693134862316E+308

And you could say, what is the purpose of using an Infinity number? Well, the question is so big and maybe I will not be able to answer to that but I will try to show you a little example where we could see the purpose of using an Infinity number.

We have the next example where we need to get the second-highest number with the array variable and maybe the first approach that we have maybe could be the next.

let input = [3, 1, 4, 2, 5];

function secondHighest(array) {
  let highest = Math.max(...array);
  let secondHighest = 0;

  for (let i = 0; i < array.length; i++) {
    if (array[i] > secondHighest && array[i] < highest) {
      secondHighest = array[i];
    }
  }

  return secondHighest;
}
Enter fullscreen mode Exit fullscreen mode

The previous code will be working correctly and we will get the output: 4 as we expected. That is fine but, what happens when we have negative numbers?

let input = [-3, -1, -4, -2, -5];
Enter fullscreen mode Exit fullscreen mode

Maybe we could set on the conditional the lowest number than the rest of the negative numbers on the array but that's not the best solution because we could have a problem identifying that specific number and maybe we will not get the correct output. For example, if we execute the next code as it is the output will be 0 because any other number is biggest than 0, that is, or first conditional with the variable secondHighest.

How we could resolve that problem in this case? Using a negative Infinity number.

function secondHighest(array) {
  let highest = Math.max(...array);
  let secondHighest = -Infinity;

  for (let i = 0; i < array.length; i++) {
    if (array[i] > secondHighest && array[i] < highest) {
      secondHighest = array[i];
    }
  }

  return secondHighest;
}
Enter fullscreen mode Exit fullscreen mode

Here you will get the correct output without using any other negative number, so here you see a good example of using Infinity numbers in your next functions, components or code in general so try it and you will see how helpful is to use this amazing JavaScript tool.

Top comments (2)

Collapse
 
jonrandy profile image
Jon Randy 🎖️ • Edited
const secondHighest = (arr, highest=Math.max(...arr)) =>
  Math.max(...arr.filter(n => n!=highest))
Enter fullscreen mode Exit fullscreen mode
Collapse
 
jesusjimenezc profile image
Jesus Jimenez Cordero

That's a good solution to this scenario but the porpouse of that example is show how helpful is the Infinity number in Javascript. Thank you for the comment and the solution.