In TypeScript, the Number.isFinite()
method of the Number
class is used to check if a certain number is
Note: TypeScript is the superset of JavaScript. Therefore, this method also exists in JavaScript.
Number.isFinite(number)
number
: This is the number we want to check for finite values.
It returns true
if the value is finite. Otherwise, it returns false
.
// create some numbers in TypeScriptlet num1:number = 3224let num2:number = 0/0let num3:number = Number.POSITIVE_INFINITYlet num4:number = Number.NaNlet num5:number = 100// check if they are finiteconsole.log(Number.isFinite(num1)) // trueconsole.log(Number.isFinite(num2)) // falseconsole.log(Number.isFinite(num3)) // falseconsole.log(Number.isFinite(num4)) // falseconsole.log(Number.isFinite(num5)) // true
Lines 2–6: We use the number
datatype to create and initialize a few variables.
Lines 4: The Number.POSITIVE_INFINITY
property represents the positive infinity value. Its value is higher than any other number. It is a property of theNumber
object.
Line 5: The Number.NaN
property represents "Not-A-Number", meaning NaN
is not a legal number. It returns true if the value's type is Number
and NaN
.
Lines 9–13: We use the Number.isFinite()
method to check if the numbers are finite. We print the results to the console.
Free Resources