JavaScript: Infinity War

JavaScript: Infinity War

Infinity or -Infinity is a predefined global value that JavaScript automatically assigns to a variable when the math calculation becomes infinite.

The type of Infinity is a Number.

When it happens users of your web app get to see 'Infinity'.

It can happen with mathematical functions like,

Math.pow(10, 309) // Exactly changes to infinity at 10^309
Math.log(0) // Infinity
(1/0) // Infinity
-(1/0) // -Infinity

(1/Infinity) // 0

typeof Infinity === Number // true

To show users a more friendly "N/A", currently you can validate Infinity in the following ways.

Notice the difference in isFinite() and Number.isFinite(). The later is more robust in checking for finite values.

But both of them fails to check just for Infinity values reliably. This requires our custom isInfinity() method added to your project. You can use it when you do some mathematical calculations.

After solving JavaScript's Infinity War, now we all deserve to watch Avengers: Infinity War

Watch Marvel Studios’ Avengers: Infinity War | Full Movie | Disney+
The Avengers must be willing to sacrifice all to defeat Thanos.
Disney+ subscription needed
Show Comments