Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

typescript

# What is the Number.isFinite() method in TypeScript? Theodore Kelechukwu Onyejiaku

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

### Overview

In TypeScript, the Number.isFinite() method of the Number class is used to check if a certain number is finiteA finite number is a number that is measurable or countable .

Note: TypeScript is the superset of JavaScript. Therefore, this method also exists in JavaScript.

### Syntax

Number.isFinite(number)
The syntax for isFinite() in TypeScript

### 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
Check if a number is finite in TypeScript

### Explanation

• 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.

RELATED TAGS

typescript

CONTRIBUTOR Theodore Kelechukwu Onyejiaku

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring

Learn in-demand tech skills in half the time 