How to check if a number is infinity in JavaScript
Learn, how to check if a given number is infinity or not in JavaScript.
Using the Number.isFinite() method
The Number.isFinite()
method accepts the value as a argument and returns false
if a value is +Infinity, -Infinity, or NaN (Not-a-Number); else it returns true
.
Example:
Number.isInFinite(1/0); // false (Infinity)
Number.isInfinite(2/34); // true (finite number)
Similarly, we can also use the Global isFinite()
method but it coerces the given value to a number first.
isFinite(1/'13'); // true
isFinite(3/0); // false (Infinity)