Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How does JavaScript determine whether a variable is a number?

Shulou Source: shulou.com Published: 2022-05-31 13:07:20 09月29日 Update

This article introduces the relevant knowledge of "how to judge whether a variable is a number or not". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

First create some test variables:

Let intVar = 2; let floatVar = 10.5; let stringVar = '4mm; let nanVar = NaN; let infinityVar = Infinity; let nullnullVar = null; let undefinedundefinedVar = undefined

Use the Number.isFinite () function

Number.isFinite () is used by the function to check whether a variable is a number, but it is also used to check if it is some special value. It returns 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 variables of non-numeric types will be ignored. If you want to check whether a variable is a number, the Number.isFinite () function is the best choice.

Use the Number.isNaN () function

A 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 a non-operator in the check.

Now let's see if you can filter only numbers by adding the Number.isNaN () function to a non-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 quite lenient because the value it accepts is not a number at all. This method works best if you know that your value is a number and want to check if it is a NaN value, but it is not suitable for regular numbers.

Use the typeof () function

The typeof () function is a global function whose arguments can take a variable or value 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)

To verify that the variable is numeric, we just need to check that the value returned by typeof () is "number". Let's try the test variable:

> typeof (intVar) = = 'number'; true > typeof (floatVar) = =' number'; true > typeof (stringVar) = = 'number'; false > typeof (nanVar) = =' number'; true # judgment error > typeof (infinityVar) = = 'number'; true # judgment error > 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 returns true.

This is the end of the content of "how to determine whether a variable is a number by JavaScript". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Tags: Number variable function check error type special object method test next content parameter situation more knowledge operator operation practical loose Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology MySQL Shulou Information Xiaomi macOS