What is the Number.isFinite() method in TypeScript?
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.
Syntax
Number.isFinite(number)
Parameters
number: This is the number we want to check for finite values.
Return value
It returns true if the value is finite. Otherwise, it returns false.
Example
// 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
Explanation
Lines 2–6: We use the
numberdatatype to create and initialize a few variables.Lines 4: The
Number.POSITIVE_INFINITYproperty represents the positive infinity value. Its value is higher than any other number. It is a property of theNumberobject.Line 5: The
Number.NaNproperty represents "Not-A-Number", meaningNaNis not a legal number. It returns true if the value's type isNumberandNaN.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