Several ways to check whether JavaScript variables are numbers

Several ways to check whether JavaScript variables are numbers

Front-end Pioneer

Several ways to check whether JavaScript variables are numbers

Introduction


JavaScript is a dynamically typed language, which means that the interpreter determines the variable type at runtime. This allows us to store different types of data in the same variable. But if there is no documentation and consistency, when using the code, we may not know what type of variable is.

When we intend to operate on numbers, it will bring strange results if we operate on strings or arrays. In this article, we will study various functions that can help us determine whether the variables used are numbers.

String-form numbers such as "100" should not be processed, and special values ​​such as NaN, Infinity, and -Infinity in JavaScript are also numbers, but we will ignore these values.

According to these requirements, it is best to use the built-in isFinite() function of the Number object. But sometimes we also use other functions, such as Number.isNaN() and typeof().

First create some test variables:


let intVar = 2;
let floatVar = 10.5;
let stringVar = '4';
let nanVar = NaN;
let infinityVar = Infinity;
let nullVar = null;
let undefinedVar = undefined;

Use Number.isFinite() function


Number.isFinite() is used to check whether the variable is a number, but also to check whether it is some special value. It will return false when it encounters NaN, Infinity or -Infinity.

Next, test on the variables defined above:

Number.isFinite(intVar);
true
Number.isFinite(floatVar);
true
Number.isFinite(stringVar);
false
Number.isFinite(nanVar);
false
Number.isFinite(infinityVar);
false
Number.isFinite(nullVar);
false
Number.isFinite(undefined);
false

The result is satisfactory. Special numeric values ​​and all non-numeric variables will be ignored. If you want to check whether a variable is a number, the Number.isFinite() function is the best choice.

Use Number.isNaN() function


The standard Number object has an isNaN() method. Used to determine whether the passed parameter value is NaN. Since we want to check whether the variable is a number, we need to use the NOT operator! in the check.

Now let's see if we can filter only numbers by adding the Number.isNaN() function with the not operator:

!Number.isNaN(intVar);
true
!Number.isNaN(floatVar);
true
!Number.isNaN(stringVar);
true # judgment error
! Number.isNaN(nanVar);
false
!Number.isNaN(infinityVar);
true # judgment Error
! Number.isNaN(nullVar);
true # judgment error
! Number.isNaN(undefinedVar);
true # judgment error

This method is fairly lenient because the value it accepts is not a number at all. This method is most suitable when you know that your value is a number and you want to check whether it is a NaN value. It is not suitable for regular numbers.

Use typeof() function


The typeof() function is a global function. Its parameters can accept variables or values ​​and return a string representation of its type. There are 9 types of JavaScript:

  • undefined
  • boolean
  • number
  • string
  • bigint
  • symbol
  • object
  • null (typeof() is displayed as an object)
  • function (a special type of object)
    In order to verify whether the variable is a number, we only need to check whether the value returned by typeof() is "number". Let's try a test variable:

typeof(intVar) == 'number';
true
typeof(floatVar) == 'number';
true
typeof(stringVar) == 'number';
false
typeof(nanVar) == 'number';
true # 判断错误
typeof(infinityVar) == 'number';
true # 判断错误
typeof(nullVar) == 'number';
false
typeof(undefined) == 'number';
false

The typeof() function is much better than Number.isNaN(). It can correctly judge that null and undefined are not numbers. But if it is NaN and Infinity, it will return true.

Although this is technically correct, NaN and Infinity are special numeric values ​​and we ignore them in most cases.

to sum up


This article studied how to check whether a variable in JavaScript is a number.

  • Only when we know that our variable is a number and need to verify whether it is NaN, the Number.isNaN() function is applicable.

  • If your code needs to deal with NaN, Infinity or -Infinity and other numbers, the typeof() function is applicable.

  • The Number.isFinite() method can handle special numbers and is best suited to our requirements.

Guess you like

Origin blog.51cto.com/15077562/2609666