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 use JavaScript to determine whether a variable is a number

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article mainly explains "how to use JavaScript to judge whether a variable is a number", the content of the explanation in the article is simple and clear, easy to learn and understand, now please follow the editor's train of thought slowly in depth, together to study and learn "how to use JavaScript to determine whether a variable is a number" bar!

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.

Thank you for your reading, the above is the content of "how to use JavaScript to judge whether a variable is a number". After the study of this article, I believe you have a deeper understanding of how to use JavaScript to determine whether a variable is a number, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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