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 does JavaScript determine whether a variable is a number?

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

Share

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

Today, I would like to share with you the relevant knowledge points about how JavaScript determines whether a variable is a number. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article. Let's take a look.

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.

These are all the contents of the article "how to determine whether a variable is a number by JavaScript". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more 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