Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to write the code that JavaScript determines whether the variable is a number or not?

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

Most people do not understand the knowledge points of this article, "JavaScript determines whether the variable is a numeric code." so the editor summarizes the following contents, detailed contents, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "JavaScript to determine whether the variable is a numeric code."

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.

The above is about the content of this article "how to write the code that JavaScript judges whether the variable is a number or not". I believe we all have some understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report