The isFinite method is used to check if the value passed to the function is a finite number or not. The isFinite method is available in the window object.
A value is considered a
finitevalue, if the value is not equal toPositive Infinity,Negative Infinity,NaN, andundefined.
isFinite(val)
The argument
valwill be be converted toNumberif the passed value is not a type ofNumber.
The isFinite method returns true if the value passed is a finite value; otherwise it returns false.
console.log("0 :", isFinite(0)); // trueconsole.log("\n10.12 :", isFinite(10.12)); // trueconsole.log("\n-10 : ", isFinite(-10)); // trueconsole.log("\nInfinity :", isFinite(Infinity)); // falseconsole.log("\n-Infinity :", isFinite(-Infinity)); // falseconsole.log("\nNaN :", isFinite(NaN)); // falseconsole.log("\nNaN :", isFinite(undefined)); // falseconsole.log("\n'0' :", isFinite('0'));console.log("\nnull : ", isFinite(null));console.log("\nnull : ", isFinite('test'));
We use the isFinite() method to check if the values are finite.
For the numbers 0, 10.12, -10 isFinite method returns true. This is because they are all finite numbers.
For the numbers +Infinity, -Infinity, NaN and undefined, the isFinite method returns false. This is because they are all infinite numbers.
For the '0' string, the isFinite method will first convert the '0' string to a number. If '0' is converted to a number we will get 0, which is a finite value. So, true will be returned.
For null, the isFinite method will first convert the null to a number. If null is converted to a number we will get 0, which is a finite value. So, true will be returned.
For the test string, the isFinite method will first convert the test string to a number. If test is converted to a number we will get NaN, which is an infinite value. So, false will be returned.