YogeshChauhan . com

The difference between isFinite() method and isFinite() function in JavaScript

in JavaScript on July 8, 2020

JavaScript isFinite() Function

The isFinite() function determines whether a number is a finite, legal number. 

If  needed, the parameter is first converted to a number.

This function returns false if the value is +infinity, -infinity, or NaN (Not-a-Number), otherwise it returns true.

Syntax


isFinite(testValue)

where testValue = The value to be tested for finiteness.

Examples


console.log(isFinite(1)) //true
console.log(isFinite(-1.99)) //true
console.log(isFinite(4-2)) //true
console.log(isFinite(0)) //true
console.log(isFinite("1")) //true
console.log(isFinite("string")) //false
console.log(isFinite("2020/5/5")) //false

JavaScript isFinite() Method

Checks whether a value is a finite number.

This method returns true if the value is of the type Number, and equates to a finite number. Otherwise it returns false.

Examples


Number.isFinite(1) //true
Number.isFinite(-1.1) //true
Number.isFinite(4-2) //true
Number.isFinite(0) //true
Number.isFinite('1') //false
Number.isFinite('String') //false
Number.isFinite('2020/5/5') //false
Number.isFinite(Infinity) //false
Number.isFinite(-Infinity) //false
Number.isFinite(0 / 0) //false

You must have noticed the difference.

If not, let me show here separately.


//function
console.log(isFinite("1")) //true

//method
Number.isFinite('1') //false

//in case you're thinking about quotes, they don't matter. You can use either one.

Almost everything is the same about the method and function, except one thing.

The global isFinite() function converts the tested value to a Number, then tests it.

Number.isFinite() does not convert the values to a Number, and will not return true for any value that is not of the type Number.

amazon

Most Read

#1 How to check if radio button is checked or not using JavaScript? #2 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #3 How to add Read More Read Less Button using JavaScript? #4 How to uninstall Cocoapods from the Mac OS? #5 How to Use SQL MAX() Function with Dates? #6 PHP Login System using PDO Part 1: Create User Registration Page

Recently Posted

Jun 16 What are Stored Procedures for SQL Server? Jun 16 What are Class Constants in PHP? Jun 15 A short basic guide on states in React Jun 15 How to define constants in PHP? Jun 15 How to define visibility for a property in PHP? Jun 15 How to use @if and @else in SCSS?

You might also like these

RBV Framework and closing of big brandsMiscHow to import a CSS file using PHP code and not HTML code?PHPWordPress: How to find all posts from a custom post type with specific custom field?WordPressSolution to “TypeError: ‘x’ is not iterable” in Angular 9AngularHow to Block IPs and User Agents using code in Drupal or WordPress?DrupalHow to pass arguments in SCSS function?SCSS