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 TypeScript
let num1:number = 3224
let num2:number = 0/0
let num3:number = Number.POSITIVE_INFINITY
let num4:number = Number.NaN
let num5:number = 100
// check if they are finite
console.log(Number.isFinite(num1)) // true
console.log(Number.isFinite(num2)) // false
console.log(Number.isFinite(num3)) // false
console.log(Number.isFinite(num4)) // false
console.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