What is Number.isFinite() in JavaScript?
The Number.isFinite method is used to check if the number passed to the function is a finite number or not.
The argument is considered a
finitenumber if the argument is not equal toPositive InfinityandNegative InfinityandNaN.
Syntax
Number.isFinite(val)
The argument
valmust be a type ofNumber. Otherwise,falsewill be returned.
Return value
The Number.isFinite method returns true if the number passed is a finite number; otherwise, it returns false.
Example
console.log("0 :", Number.isFinite(0)); // trueconsole.log("\n10.12 :", Number.isFinite(10.12)); // trueconsole.log("\n-10 : ", Number.isFinite(-10)); // trueconsole.log("\nInfinity :", Number.isFinite(Infinity)); // falseconsole.log("\n-Infinity :", Number.isFinite(-Infinity)); // falseconsole.log("\nNaN :", Number.isFinite(NaN)); // falseconsole.log("\n'0' :", Number.isFinite('0')); // false, because '0' is a string not a numberconsole.log("\nnull : ", Number.isFinite(null)); // false, because 'null' is not a number
We use the Number.isFinite() method to check if the numbers are finite.
-
For the numbers
0,10.12,-10, theisFinitemethod returnstrue. This is because they are all finite numbers. -
For the numbers
+Infinity,-Infinity,NaN, theisFinitemethod returnsfalse. This is because they are all infinite numbers. -
For the
'0'string, theisFinitemethod returnsfalse. This is because'0'is not aNumbertype. -
For
null, theisFinitemethod returnsfalse. This is becausenullis not aNumbertype.