RAMANA MURTHY G
Mega Sage

Hello folks,

It’s all about Infinity in JavaScript.

 

Wait, what is INFINITY?? Ugh??

 

In JavaScript, INFINITY is a special numeric value that represents Positive Infinity. It is greater than any other number and is a part of the Global Scope (Global Variable). There is also -INFINITY, which represents Negative Infinity.

 

INFINITY behaves similarly to mathematical Infinity. It is typically the result of a number exceeds the largest representable number in JavaScript or when you divide a non-zero number by Zero.

INFINITY is a property of global object just like NaN or undefined

 

e.g.:

gs.info(1/0);        // which prints INFINITY

gs.info(-1/0);       // which prints -INFINITY

 

let's assume another example. I wanted to find a maximum element in an array [3, 4, 5, 2, 7, 1];

var number = [3, 4, 5, 2, 7, 1];

let max = -Infinity;     // negative infinity

for (let i = 0; i < number.length; i++) {
  if (number[i] > max) {
    max = number[i];
  }
}
gs.info("Largest number is:", max);

// Output: Largest number is: 7

 

Note: We use -INFINITY so that any number in the array will be greater than the initial max.

 

 

Please mark as helpful, if you found this article valuable and useful

Thank you

 

Ramana Murthy G

ServiceNow Developer.

Version history
Last update:
‎07-01-2025 08:14 AM
Updated by:
Contributors