In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)05/31 Report--
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!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.