Home > Web Front-end > JS Tutorial > body text

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

不言
Release: 2019-01-16 16:30:37
Original
3829 people have browsed it

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));
Copy after login

Execution result:

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

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"));
Copy after login

Execution result:

isFinite("abc") = false
isFinite("123abc") = false
isFinite("123") = true
Copy after login

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));
Copy after login

Execution results:

isFinite(true) = true
isFinite(false) = true
Copy after login

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));
Copy after login

Execution result:

isFinite(Infinity) = false
isFinite(undefined) = false
isFinite(null) = true
isFinite(NaN) = false
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template