Home >Web Front-end >JS Tutorial >How to check if it is a finite numerical value using isFinite in JavaScript

How to check if it is a finite numerical value using isFinite in JavaScript

不言
不言Original
2019-01-16 16:30:123855browse

The isFinite function in How to check if it is a finite numerical value using isFinite in JavaScript can be used to check values. If the specified value is a finite value, isFinite returns true, otherwise it returns false. In this article, we will take a look at the specific usage of the isFinite function in How to check if it is a finite numerical value using isFinite in JavaScript.

How to check if it is a finite numerical value using isFinite in JavaScript

How to use isFinite function?

Methods to check numbers

To use isFinite to check numbers, you need to specify the value to be checked in the parameter.

isFinite returns true for the value, but returns false for the array. This needs to be noted.

Let’s look at the following code

console.log('isFinite(123) = ' + isFinite(123));
console.log('isFinite([1, 2]) = ' + isFinite([1, 2]));
console.log('isFinite(0755) = ' + isFinite(0755));
console.log('isFinite(0xFF) = ' + isFinite(0xFF));

Execution result:

isFinite(123) = true
isFinite([1, 2]) = false
isFinite(0755) = true
isFinite(0xFF) = true

Methods for checking strings

isFinite returns "true" if it is a numeric value when converting the string to a numeric value.

If the value contains a string, return "false".

Let’s take a look at the following program.

console.log('isFinite("abc") = ' + isFinite("abc"));
console.log('isFinite("123abc") = ' + isFinite("123abc"));
console.log('isFinite("123") = ' + isFinite("123"));

Execution result:

isFinite("abc") = false
isFinite("123abc") = false
isFinite("123") = true

Method to check Boolean type

If it is a Boolean type, isFinite returns "true".

console.log('isFinite(true) = ' + isFinite(true));
console.log('isFinite(false) = ' + isFinite(false));

Execution results:

isFinite(true) = true
isFinite(false) = true

How to check null

How to use isFinite to check special values, such as null

If null, isFinite returns "true".

In other cases of "Infinity", "undefined", "NaN", it returns "false".

Let’s take a look at the following program.

console.log('isFinite(Infinity) = ' + isFinite(Infinity));
console.log('isFinite(undefined) = ' + isFinite(undefined));
console.log('isFinite(null) = ' + isFinite(null));
console.log('isFinite(NaN) = ' + isFinite(NaN));

Execution result:

isFinite(Infinity) = false
isFinite(undefined) = false
isFinite(null) = true
isFinite(NaN) = false

The above is the detailed content of How to check if it is a finite numerical value using isFinite in JavaScript. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn